2008-01-08 01:52:51 +02:00
|
|
|
/*
|
|
|
|
* Linux Media Labs MPEG-4 demuxer
|
|
|
|
* Copyright (c) 2008 Ivo van Poorten
|
|
|
|
*
|
|
|
|
* Due to a lack of sample files, only files with one channel are supported.
|
|
|
|
* u-law and ADPCM audio are unsupported for the same reason.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2008-01-08 01:52:51 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-01-08 01:52:51 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-01-08 01:52:51 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-01-08 01:52:51 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2014-09-23 17:29:58 +03:00
|
|
|
|
2008-01-08 01:52:51 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2008-01-08 01:52:51 +02:00
|
|
|
|
|
|
|
#define LMLM4_I_FRAME 0x00
|
|
|
|
#define LMLM4_P_FRAME 0x01
|
|
|
|
#define LMLM4_B_FRAME 0x02
|
|
|
|
#define LMLM4_INVALID 0x03
|
|
|
|
#define LMLM4_MPEG1L2 0x04
|
|
|
|
|
|
|
|
#define LMLM4_MAX_PACKET_SIZE 1024 * 1024
|
|
|
|
|
2014-09-23 17:29:58 +03:00
|
|
|
static int lmlm4_probe(AVProbeData *pd)
|
|
|
|
{
|
2008-01-08 01:52:51 +02:00
|
|
|
unsigned char *buf = pd->buf;
|
|
|
|
unsigned int frame_type, packet_size;
|
|
|
|
|
2014-09-23 17:29:58 +03:00
|
|
|
frame_type = AV_RB16(buf + 2);
|
|
|
|
packet_size = AV_RB32(buf + 4);
|
2008-01-08 01:52:51 +02:00
|
|
|
|
|
|
|
if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
|
|
|
|
frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
|
|
|
|
if (frame_type == LMLM4_MPEG1L2) {
|
2014-09-23 17:29:58 +03:00
|
|
|
if ((AV_RB16(buf + 8) & 0xfffe) != 0xfffc)
|
2008-01-08 01:52:51 +02:00
|
|
|
return 0;
|
|
|
|
/* I could calculate the audio framesize and compare with
|
|
|
|
* packet_size-8, but that seems overkill */
|
|
|
|
return AVPROBE_SCORE_MAX / 3;
|
2014-09-23 17:29:58 +03:00
|
|
|
} else if (AV_RB24(buf + 8) == 0x000001) { /* PES Signal */
|
2008-01-08 01:52:51 +02:00
|
|
|
return AVPROBE_SCORE_MAX / 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:29:58 +03:00
|
|
|
static int lmlm4_read_header(AVFormatContext *s)
|
|
|
|
{
|
2008-01-08 01:52:51 +02:00
|
|
|
AVStream *st;
|
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
if (!(st = avformat_new_stream(s, NULL)))
|
2008-01-08 01:52:51 +02:00
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_MPEG4;
|
2008-01-08 01:52:51 +02:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_HEADERS;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, 1001, 30000);
|
2008-01-08 01:52:51 +02:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
if (!(st = avformat_new_stream(s, NULL)))
|
2008-01-08 01:52:51 +02:00
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_MP2;
|
2008-01-08 01:52:51 +02:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_HEADERS;
|
|
|
|
|
|
|
|
/* the parameters will be extracted from the compressed bitstream */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:29:58 +03:00
|
|
|
static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-01-08 01:52:51 +02:00
|
|
|
int ret;
|
|
|
|
unsigned int frame_type, packet_size, padding, frame_size;
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_rb16(pb); /* channel number */
|
|
|
|
frame_type = avio_rb16(pb);
|
|
|
|
packet_size = avio_rb32(pb);
|
2008-01-08 01:52:51 +02:00
|
|
|
padding = -packet_size & 511;
|
|
|
|
frame_size = packet_size - 8;
|
|
|
|
|
|
|
|
if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
if (packet_size > LMLM4_MAX_PACKET_SIZE) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, padding);
|
2008-01-08 01:52:51 +02:00
|
|
|
|
|
|
|
switch (frame_type) {
|
2014-09-23 17:29:58 +03:00
|
|
|
case LMLM4_I_FRAME:
|
|
|
|
pkt->flags = AV_PKT_FLAG_KEY;
|
|
|
|
case LMLM4_P_FRAME:
|
|
|
|
case LMLM4_B_FRAME:
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
break;
|
|
|
|
case LMLM4_MPEG1L2:
|
|
|
|
pkt->stream_index = 1;
|
|
|
|
break;
|
2008-01-08 01:52:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_lmlm4_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "lmlm4",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("raw lmlm4"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.read_probe = lmlm4_probe,
|
|
|
|
.read_header = lmlm4_read_header,
|
|
|
|
.read_packet = lmlm4_read_packet,
|
2008-01-08 01:52:51 +02:00
|
|
|
};
|