Libav
|
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 00028 #define LMLM4_I_FRAME 0x00 00029 #define LMLM4_P_FRAME 0x01 00030 #define LMLM4_B_FRAME 0x02 00031 #define LMLM4_INVALID 0x03 00032 #define LMLM4_MPEG1L2 0x04 00033 00034 #define LMLM4_MAX_PACKET_SIZE 1024 * 1024 00035 00036 static int lmlm4_probe(AVProbeData * pd) { 00037 unsigned char *buf = pd->buf; 00038 unsigned int frame_type, packet_size; 00039 00040 frame_type = AV_RB16(buf+2); 00041 packet_size = AV_RB32(buf+4); 00042 00043 if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size && 00044 frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) { 00045 00046 if (frame_type == LMLM4_MPEG1L2) { 00047 if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc) 00048 return 0; 00049 /* I could calculate the audio framesize and compare with 00050 * packet_size-8, but that seems overkill */ 00051 return AVPROBE_SCORE_MAX / 3; 00052 } else if (AV_RB24(buf+8) == 0x000001) { /* PES Signal */ 00053 return AVPROBE_SCORE_MAX / 5; 00054 } 00055 } 00056 00057 return 0; 00058 } 00059 00060 static int lmlm4_read_header(AVFormatContext *s, AVFormatParameters *ap) { 00061 AVStream *st; 00062 00063 if (!(st = av_new_stream(s, 0))) 00064 return AVERROR(ENOMEM); 00065 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00066 st->codec->codec_id = CODEC_ID_MPEG4; 00067 st->need_parsing = AVSTREAM_PARSE_HEADERS; 00068 av_set_pts_info(st, 64, 1001, 30000); 00069 00070 if (!(st = av_new_stream(s, 1))) 00071 return AVERROR(ENOMEM); 00072 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00073 st->codec->codec_id = CODEC_ID_MP2; 00074 st->need_parsing = AVSTREAM_PARSE_HEADERS; 00075 00076 /* the parameters will be extracted from the compressed bitstream */ 00077 return 0; 00078 } 00079 00080 static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) { 00081 ByteIOContext *pb = s->pb; 00082 int ret; 00083 unsigned int frame_type, packet_size, padding, frame_size; 00084 00085 get_be16(pb); /* channel number */ 00086 frame_type = get_be16(pb); 00087 packet_size = get_be32(pb); 00088 padding = -packet_size & 511; 00089 frame_size = packet_size - 8; 00090 00091 if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) { 00092 av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n"); 00093 return AVERROR(EIO); 00094 } 00095 if (packet_size > LMLM4_MAX_PACKET_SIZE) { 00096 av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n"); 00097 return AVERROR(EIO); 00098 } 00099 00100 if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0) 00101 return AVERROR(EIO); 00102 00103 url_fskip(pb, padding); 00104 00105 switch (frame_type) { 00106 case LMLM4_I_FRAME: 00107 pkt->flags = AV_PKT_FLAG_KEY; 00108 case LMLM4_P_FRAME: 00109 case LMLM4_B_FRAME: 00110 pkt->stream_index = 0; 00111 break; 00112 case LMLM4_MPEG1L2: 00113 pkt->stream_index = 1; 00114 break; 00115 } 00116 00117 return ret; 00118 } 00119 00120 AVInputFormat lmlm4_demuxer = { 00121 "lmlm4", 00122 NULL_IF_CONFIG_SMALL("lmlm4 raw format"), 00123 0, 00124 lmlm4_probe, 00125 lmlm4_read_header, 00126 lmlm4_read_packet, 00127 };