lmlm4.c 3.96 KB
Newer Older
1 2 3 4 5 6 7
/*
 * 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.
 *
8
 * This file is part of Libav.
9
 *
10
 * Libav is free software; you can redistribute it and/or
11 12 13 14
 * 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.
 *
15
 * Libav is distributed in the hope that it will be useful,
16 17 18 19 20
 * 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
21
 * License along with Libav; if not, write to the Free Software
22 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

25
#include "libavutil/intreadwrite.h"
26
#include "avformat.h"
27
#include "internal.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#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;
}

61
static int lmlm4_read_header(AVFormatContext *s) {
62 63
    AVStream *st;

64
    if (!(st = avformat_new_stream(s, NULL)))
65
        return AVERROR(ENOMEM);
66
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
67
    st->codec->codec_id   = AV_CODEC_ID_MPEG4;
68
    st->need_parsing      = AVSTREAM_PARSE_HEADERS;
69
    avpriv_set_pts_info(st, 64, 1001, 30000);
70

71
    if (!(st = avformat_new_stream(s, NULL)))
72
        return AVERROR(ENOMEM);
73
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
74
    st->codec->codec_id   = AV_CODEC_ID_MP2;
75 76 77 78 79 80 81
    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) {
82
    AVIOContext *pb = s->pb;
83 84 85
    int ret;
    unsigned int frame_type, packet_size, padding, frame_size;

86 87 88
    avio_rb16(pb);                       /* channel number */
    frame_type  = avio_rb16(pb);
    packet_size = avio_rb32(pb);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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);

104
    avio_skip(pb, padding);
105 106 107

    switch (frame_type) {
        case LMLM4_I_FRAME:
108
            pkt->flags = AV_PKT_FLAG_KEY;
109 110 111 112 113 114 115 116 117 118 119 120
        case LMLM4_P_FRAME:
        case LMLM4_B_FRAME:
            pkt->stream_index = 0;
            break;
        case LMLM4_MPEG1L2:
            pkt->stream_index = 1;
            break;
    }

    return ret;
}

121
AVInputFormat ff_lmlm4_demuxer = {
122
    .name           = "lmlm4",
123
    .long_name      = NULL_IF_CONFIG_SMALL("raw lmlm4"),
124 125 126
    .read_probe     = lmlm4_probe,
    .read_header    = lmlm4_read_header,
    .read_packet    = lmlm4_read_packet,
127
};