Statistics
| Branch: | Revision:

ffmpeg / libavformat / lmlm4.c @ 9dd94f83

History | View | Annotate | Download (3.86 KB)

1 670c7847 Ivo van Poorten
/*
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 2912e87a Mans Rullgard
 * This file is part of Libav.
9 670c7847 Ivo van Poorten
 *
10 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
11 670c7847 Ivo van Poorten
 * 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 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
16 670c7847 Ivo van Poorten
 * 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 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
22 670c7847 Ivo van Poorten
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
26 670c7847 Ivo van Poorten
#include "avformat.h"
27
28
#define LMLM4_I_FRAME   0x00
29
#define LMLM4_P_FRAME   0x01
30
#define LMLM4_B_FRAME   0x02
31
#define LMLM4_INVALID   0x03
32
#define LMLM4_MPEG1L2   0x04
33
34
#define LMLM4_MAX_PACKET_SIZE   1024 * 1024
35
36
static int lmlm4_probe(AVProbeData * pd) {
37
    unsigned char *buf = pd->buf;
38
    unsigned int frame_type, packet_size;
39
40
    frame_type  = AV_RB16(buf+2);
41
    packet_size = AV_RB32(buf+4);
42
43
    if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
44
        frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
45
46
        if (frame_type == LMLM4_MPEG1L2) {
47
            if ((AV_RB16(buf+8) & 0xfffe) != 0xfffc)
48
                return 0;
49
            /* I could calculate the audio framesize and compare with
50
             * packet_size-8, but that seems overkill */
51
            return AVPROBE_SCORE_MAX / 3;
52
        } else if (AV_RB24(buf+8) == 0x000001) {    /* PES Signal */
53
            return AVPROBE_SCORE_MAX / 5;
54
        }
55
    }
56
57
    return 0;
58
}
59
60
static int lmlm4_read_header(AVFormatContext *s, AVFormatParameters *ap) {
61
    AVStream *st;
62
63
    if (!(st = av_new_stream(s, 0)))
64
        return AVERROR(ENOMEM);
65 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
66 670c7847 Ivo van Poorten
    st->codec->codec_id   = CODEC_ID_MPEG4;
67
    st->need_parsing      = AVSTREAM_PARSE_HEADERS;
68
    av_set_pts_info(st, 64, 1001, 30000);
69
70
    if (!(st = av_new_stream(s, 1)))
71
        return AVERROR(ENOMEM);
72 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
73 670c7847 Ivo van Poorten
    st->codec->codec_id   = CODEC_ID_MP2;
74
    st->need_parsing      = AVSTREAM_PARSE_HEADERS;
75
76
    /* the parameters will be extracted from the compressed bitstream */
77
    return 0;
78
}
79
80
static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt) {
81 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
82 670c7847 Ivo van Poorten
    int ret;
83
    unsigned int frame_type, packet_size, padding, frame_size;
84
85 b7effd4e Anton Khirnov
    avio_rb16(pb);                       /* channel number */
86
    frame_type  = avio_rb16(pb);
87
    packet_size = avio_rb32(pb);
88 670c7847 Ivo van Poorten
    padding     = -packet_size & 511;
89
    frame_size  = packet_size - 8;
90
91
    if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
92
        av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
93
        return AVERROR(EIO);
94
    }
95
    if (packet_size > LMLM4_MAX_PACKET_SIZE) {
96
        av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
97
        return AVERROR(EIO);
98
    }
99
100
    if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
101
        return AVERROR(EIO);
102
103 45a8a02a Anton Khirnov
    avio_skip(pb, padding);
104 670c7847 Ivo van Poorten
105
    switch (frame_type) {
106
        case LMLM4_I_FRAME:
107 cc947f04 Jean-Daniel Dupas
            pkt->flags = AV_PKT_FLAG_KEY;
108 670c7847 Ivo van Poorten
        case LMLM4_P_FRAME:
109
        case LMLM4_B_FRAME:
110
            pkt->stream_index = 0;
111
            break;
112
        case LMLM4_MPEG1L2:
113
            pkt->stream_index = 1;
114
            break;
115
    }
116
117
    return ret;
118
}
119
120 c6610a21 Diego Elio Pettenò
AVInputFormat ff_lmlm4_demuxer = {
121 670c7847 Ivo van Poorten
    "lmlm4",
122 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("lmlm4 raw format"),
123 670c7847 Ivo van Poorten
    0,
124
    lmlm4_probe,
125
    lmlm4_read_header,
126
    lmlm4_read_packet,
127
};