• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/lmlm4.c

Go to the documentation of this file.
00001 /*
00002  * Linux Media Labs MPEG-4 demuxer
00003  * Copyright (c) 2008 Ivo van Poorten
00004  *
00005  * Due to a lack of sample files, only files with one channel are supported.
00006  * u-law and ADPCM audio are unsupported for the same reason.
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 #include "libavutil/intreadwrite.h"
00026 #include "avformat.h"
00027 #include "internal.h"
00028 
00029 #define LMLM4_I_FRAME   0x00
00030 #define LMLM4_P_FRAME   0x01
00031 #define LMLM4_B_FRAME   0x02
00032 #define LMLM4_INVALID   0x03
00033 #define LMLM4_MPEG1L2   0x04
00034 
00035 #define LMLM4_MAX_PACKET_SIZE   1024 * 1024
00036 
00037 static int lmlm4_probe(AVProbeData * pd) {
00038     unsigned char *buf = pd->buf;
00039     unsigned int frame_type, packet_size;
00040 
00041     frame_type  = AV_RB16(buf+2);
00042     packet_size = AV_RB32(buf+4);
00043 
00044     if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
00045         frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
00046 
00047         if (frame_type == LMLM4_MPEG1L2) {
00048             if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc)
00049                 return 0;
00050             /* I could calculate the audio framesize and compare with
00051              * packet_size-8, but that seems overkill */
00052             return AVPROBE_SCORE_MAX / 3;
00053         } else if (AV_RB24(buf+8) == 0x000001) {    /* PES Signal */
00054             return AVPROBE_SCORE_MAX / 5;
00055         }
00056     }
00057 
00058     return 0;
00059 }
00060 
00061 static int lmlm4_read_header(AVFormatContext *s, AVFormatParameters *ap) {
00062     AVStream *st;
00063 
00064     if (!(st = avformat_new_stream(s, NULL)))
00065         return AVERROR(ENOMEM);
00066     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00067     st->codec->codec_id   = CODEC_ID_MPEG4;
00068     st->need_parsing      = AVSTREAM_PARSE_HEADERS;
00069     avpriv_set_pts_info(st, 64, 1001, 30000);
00070 
00071     if (!(st = avformat_new_stream(s, NULL)))
00072         return AVERROR(ENOMEM);
00073     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00074     st->codec->codec_id   = CODEC_ID_MP2;
00075     st->need_parsing      = AVSTREAM_PARSE_HEADERS;
00076 
00077     /* the parameters will be extracted from the compressed bitstream */
00078     return 0;
00079 }
00080 
00081 static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) {
00082     AVIOContext *pb = s->pb;
00083     int ret;
00084     unsigned int frame_type, packet_size, padding, frame_size;
00085 
00086     avio_rb16(pb);                       /* channel number */
00087     frame_type  = avio_rb16(pb);
00088     packet_size = avio_rb32(pb);
00089     padding     = -packet_size & 511;
00090     frame_size  = packet_size - 8;
00091 
00092     if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
00093         av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
00094         return AVERROR(EIO);
00095     }
00096     if (packet_size > LMLM4_MAX_PACKET_SIZE) {
00097         av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
00098         return AVERROR(EIO);
00099     }
00100 
00101     if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
00102         return AVERROR(EIO);
00103 
00104     avio_skip(pb, padding);
00105 
00106     switch (frame_type) {
00107         case LMLM4_I_FRAME:
00108             pkt->flags = AV_PKT_FLAG_KEY;
00109         case LMLM4_P_FRAME:
00110         case LMLM4_B_FRAME:
00111             pkt->stream_index = 0;
00112             break;
00113         case LMLM4_MPEG1L2:
00114             pkt->stream_index = 1;
00115             break;
00116     }
00117 
00118     return ret;
00119 }
00120 
00121 AVInputFormat ff_lmlm4_demuxer = {
00122     .name           = "lmlm4",
00123     .long_name      = NULL_IF_CONFIG_SMALL("lmlm4 raw format"),
00124     .read_probe     = lmlm4_probe,
00125     .read_header    = lmlm4_read_header,
00126     .read_packet    = lmlm4_read_packet,
00127 };
Generated on Fri Feb 1 2013 14:34:52 for FFmpeg by doxygen 1.7.1