From 670c78479d88c86ebf1828f31a5716c54d74d257 Mon Sep 17 00:00:00 2001 From: Ivo van Poorten Date: Mon, 7 Jan 2008 23:52:51 +0000 Subject: [PATCH] Linux Media Labs MPEG-4 (LMLM4) demuxer Originally committed as revision 11456 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/lmlm4.c | 126 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 libavformat/lmlm4.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 0d69448ad5..f061376cb9 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -67,6 +67,7 @@ OBJS-$(CONFIG_IMAGE2PIPE_DEMUXER) += img2.o OBJS-$(CONFIG_IMAGE2PIPE_MUXER) += img2.o OBJS-$(CONFIG_INGENIENT_DEMUXER) += raw.o OBJS-$(CONFIG_IPMOVIE_DEMUXER) += ipmovie.o +OBJS-$(CONFIG_LMLM4_DEMUXER) += lmlm4.o OBJS-$(CONFIG_M4V_DEMUXER) += raw.o OBJS-$(CONFIG_M4V_MUXER) += raw.o OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER) += matroskaenc.o matroska.o riff.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 5435dda681..c1575eb3af 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -89,6 +89,7 @@ void av_register_all(void) REGISTER_MUXDEMUX (IMAGE2PIPE, image2pipe); REGISTER_DEMUXER (INGENIENT, ingenient); REGISTER_DEMUXER (IPMOVIE, ipmovie); + REGISTER_DEMUXER (LMLM4, lmlm4); REGISTER_MUXDEMUX (M4V, m4v); REGISTER_MUXDEMUX (MATROSKA, matroska); REGISTER_MUXER (MATROSKA_AUDIO, matroska_audio); diff --git a/libavformat/lmlm4.c b/libavformat/lmlm4.c new file mode 100644 index 0000000000..6646a412bc --- /dev/null +++ b/libavformat/lmlm4.c @@ -0,0 +1,126 @@ +/* + * 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. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * 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. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" + +#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 + +static int lmlm4_probe(AVProbeData * pd) { + unsigned char *buf = pd->buf; + unsigned int frame_type, packet_size; + + frame_type = AV_RB16(buf+2); + packet_size = AV_RB32(buf+4); + + 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) { + if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc) + return 0; + /* I could calculate the audio framesize and compare with + * packet_size-8, but that seems overkill */ + return AVPROBE_SCORE_MAX / 3; + } else if (AV_RB24(buf+8) == 0x000001) { /* PES Signal */ + return AVPROBE_SCORE_MAX / 5; + } + } + + return 0; +} + +static int lmlm4_read_header(AVFormatContext *s, AVFormatParameters *ap) { + AVStream *st; + + if (!(st = av_new_stream(s, 0))) + return AVERROR(ENOMEM); + st->codec->codec_type = CODEC_TYPE_VIDEO; + st->codec->codec_id = CODEC_ID_MPEG4; + st->need_parsing = AVSTREAM_PARSE_HEADERS; + av_set_pts_info(st, 64, 1001, 30000); + + if (!(st = av_new_stream(s, 1))) + return AVERROR(ENOMEM); + st->codec->codec_type = CODEC_TYPE_AUDIO; + st->codec->codec_id = CODEC_ID_MP2; + st->need_parsing = AVSTREAM_PARSE_HEADERS; + + /* the parameters will be extracted from the compressed bitstream */ + return 0; +} + +static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) { + ByteIOContext *pb = s->pb; + int ret; + unsigned int frame_type, packet_size, padding, frame_size; + + get_be16(pb); /* channel number */ + frame_type = get_be16(pb); + packet_size = get_be32(pb); + 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); + + url_fskip(pb, padding); + + switch (frame_type) { + case LMLM4_I_FRAME: + pkt->flags = PKT_FLAG_KEY; + case LMLM4_P_FRAME: + case LMLM4_B_FRAME: + pkt->stream_index = 0; + break; + case LMLM4_MPEG1L2: + pkt->stream_index = 1; + break; + } + + return ret; +} + +AVInputFormat lmlm4_demuxer = { + "lmlm4", + "lmlm4 raw format", + 0, + lmlm4_probe, + lmlm4_read_header, + lmlm4_read_packet, +};