LCOV - code coverage report
Current view: top level - libavformat - lmlm4.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 52 80.8 %
Date: 2017-12-12 11:08:38 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Linux Media Labs MPEG-4 demuxer
       3             :  * Copyright (c) 2008 Ivo van Poorten
       4             :  *
       5             :  * Due to a lack of sample files, only files with one channel are supported.
       6             :  * u-law and ADPCM audio are unsupported for the same reason.
       7             :  *
       8             :  * This file is part of FFmpeg.
       9             :  *
      10             :  * FFmpeg is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free Software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  *
      15             :  * FFmpeg is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  *
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with FFmpeg; if not, write to the Free Software
      22             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      23             :  */
      24             : 
      25             : #include "libavutil/intreadwrite.h"
      26             : 
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : 
      30             : #define LMLM4_I_FRAME   0x00
      31             : #define LMLM4_P_FRAME   0x01
      32             : #define LMLM4_B_FRAME   0x02
      33             : #define LMLM4_INVALID   0x03
      34             : #define LMLM4_MPEG1L2   0x04
      35             : 
      36             : #define LMLM4_MAX_PACKET_SIZE   1024 * 1024
      37             : 
      38        6130 : static int lmlm4_probe(AVProbeData *pd)
      39             : {
      40        6130 :     const unsigned char *buf = pd->buf;
      41             :     unsigned int frame_type, packet_size;
      42             : 
      43        6130 :     frame_type  = AV_RB16(buf + 2);
      44        6130 :     packet_size = AV_RB32(buf + 4);
      45             : 
      46        6130 :     if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
      47         401 :         frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
      48           1 :         if (frame_type == LMLM4_MPEG1L2) {
      49           1 :             if ((AV_RB16(buf + 8) & 0xfffe) != 0xfffc)
      50           0 :                 return 0;
      51             :             /* I could calculate the audio framesize and compare with
      52             :              * packet_size-8, but that seems overkill */
      53           1 :             return AVPROBE_SCORE_MAX / 3;
      54           0 :         } else if (AV_RB24(buf + 8) == 0x000001) {    /* PES Signal */
      55           0 :             return AVPROBE_SCORE_MAX / 5;
      56             :         }
      57             :     }
      58             : 
      59        6129 :     return 0;
      60             : }
      61             : 
      62           1 : static int lmlm4_read_header(AVFormatContext *s)
      63             : {
      64             :     AVStream *st;
      65             : 
      66           1 :     if (!(st = avformat_new_stream(s, NULL)))
      67           0 :         return AVERROR(ENOMEM);
      68           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
      69           1 :     st->codecpar->codec_id   = AV_CODEC_ID_MPEG4;
      70           1 :     st->need_parsing      = AVSTREAM_PARSE_HEADERS;
      71           1 :     avpriv_set_pts_info(st, 64, 1001, 30000);
      72             : 
      73           1 :     if (!(st = avformat_new_stream(s, NULL)))
      74           0 :         return AVERROR(ENOMEM);
      75           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
      76           1 :     st->codecpar->codec_id   = AV_CODEC_ID_MP2;
      77           1 :     st->need_parsing      = AVSTREAM_PARSE_HEADERS;
      78             : 
      79             :     /* the parameters will be extracted from the compressed bitstream */
      80           1 :     return 0;
      81             : }
      82             : 
      83         238 : static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt)
      84             : {
      85         238 :     AVIOContext *pb = s->pb;
      86             :     int ret;
      87             :     unsigned int frame_type, packet_size, padding, frame_size;
      88             : 
      89         238 :     avio_rb16(pb);                       /* channel number */
      90         238 :     frame_type  = avio_rb16(pb);
      91         238 :     packet_size = avio_rb32(pb);
      92         238 :     padding     = -packet_size & 511;
      93         238 :     frame_size  = packet_size - 8;
      94             : 
      95         238 :     if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
      96           0 :         av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
      97           0 :         return AVERROR(EIO);
      98             :     }
      99         238 :     if (packet_size > LMLM4_MAX_PACKET_SIZE || packet_size<=8) {
     100           0 :         av_log(s, AV_LOG_ERROR, "packet size %d is invalid\n", packet_size);
     101           0 :         return AVERROR(EIO);
     102             :     }
     103             : 
     104         238 :     if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
     105           0 :         return AVERROR(EIO);
     106             : 
     107         238 :     avio_skip(pb, padding);
     108             : 
     109         238 :     switch (frame_type) {
     110           6 :     case LMLM4_I_FRAME:
     111           6 :         pkt->flags = AV_PKT_FLAG_KEY;
     112          91 :     case LMLM4_P_FRAME:
     113             :     case LMLM4_B_FRAME:
     114          91 :         pkt->stream_index = 0;
     115          91 :         break;
     116         147 :     case LMLM4_MPEG1L2:
     117         147 :         pkt->stream_index = 1;
     118         147 :         break;
     119             :     }
     120             : 
     121         238 :     return ret;
     122             : }
     123             : 
     124             : AVInputFormat ff_lmlm4_demuxer = {
     125             :     .name           = "lmlm4",
     126             :     .long_name      = NULL_IF_CONFIG_SMALL("raw lmlm4"),
     127             :     .read_probe     = lmlm4_probe,
     128             :     .read_header    = lmlm4_read_header,
     129             :     .read_packet    = lmlm4_read_packet,
     130             : };

Generated by: LCOV version 1.13