FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lmlm4.c
Go to the documentation of this file.
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 static int lmlm4_probe(AVProbeData *pd)
39 {
40  const unsigned char *buf = pd->buf;
41  unsigned int frame_type, packet_size;
42 
43  frame_type = AV_RB16(buf + 2);
44  packet_size = AV_RB32(buf + 4);
45 
46  if (!AV_RB16(buf) && frame_type <= LMLM4_MPEG1L2 && packet_size &&
47  frame_type != LMLM4_INVALID && packet_size <= LMLM4_MAX_PACKET_SIZE) {
48  if (frame_type == LMLM4_MPEG1L2) {
49  if ((AV_RB16(buf + 8) & 0xfffe) != 0xfffc)
50  return 0;
51  /* I could calculate the audio framesize and compare with
52  * packet_size-8, but that seems overkill */
53  return AVPROBE_SCORE_MAX / 3;
54  } else if (AV_RB24(buf + 8) == 0x000001) { /* PES Signal */
55  return AVPROBE_SCORE_MAX / 5;
56  }
57  }
58 
59  return 0;
60 }
61 
63 {
64  AVStream *st;
65 
66  if (!(st = avformat_new_stream(s, NULL)))
67  return AVERROR(ENOMEM);
71  avpriv_set_pts_info(st, 64, 1001, 30000);
72 
73  if (!(st = avformat_new_stream(s, NULL)))
74  return AVERROR(ENOMEM);
78 
79  /* the parameters will be extracted from the compressed bitstream */
80  return 0;
81 }
82 
84 {
85  AVIOContext *pb = s->pb;
86  int ret;
87  unsigned int frame_type, packet_size, padding, frame_size;
88 
89  avio_rb16(pb); /* channel number */
90  frame_type = avio_rb16(pb);
91  packet_size = avio_rb32(pb);
92  padding = -packet_size & 511;
93  frame_size = packet_size - 8;
94 
95  if (frame_type > LMLM4_MPEG1L2 || frame_type == LMLM4_INVALID) {
96  av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
97  return AVERROR(EIO);
98  }
99  if (packet_size > LMLM4_MAX_PACKET_SIZE || packet_size<=8) {
100  av_log(s, AV_LOG_ERROR, "packet size %d is invalid\n", packet_size);
101  return AVERROR(EIO);
102  }
103 
104  if ((ret = av_get_packet(pb, pkt, frame_size)) <= 0)
105  return AVERROR(EIO);
106 
107  avio_skip(pb, padding);
108 
109  switch (frame_type) {
110  case LMLM4_I_FRAME:
111  pkt->flags = AV_PKT_FLAG_KEY;
112  case LMLM4_P_FRAME:
113  case LMLM4_B_FRAME:
114  pkt->stream_index = 0;
115  break;
116  case LMLM4_MPEG1L2:
117  pkt->stream_index = 1;
118  break;
119  }
120 
121  return ret;
122 }
123 
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 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define LMLM4_P_FRAME
Definition: lmlm4.c:31
AVInputFormat ff_lmlm4_demuxer
Definition: lmlm4.c:124
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:675
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
Format I/O context.
Definition: avformat.h:1273
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:690
static int lmlm4_read_header(AVFormatContext *s)
Definition: lmlm4.c:62
enum AVStreamParseType need_parsing
Definition: avformat.h:1034
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:244
#define LMLM4_I_FRAME
Definition: lmlm4.c:30
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
#define LMLM4_MPEG1L2
Definition: lmlm4.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#define LMLM4_MAX_PACKET_SIZE
Definition: lmlm4.c:36
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
Only parse headers, do not repack.
Definition: avformat.h:775
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
static int lmlm4_probe(AVProbeData *pd)
Definition: lmlm4.c:38
#define LMLM4_B_FRAME
Definition: lmlm4.c:32
Stream structure.
Definition: avformat.h:842
int frame_size
Definition: mxfenc.c:1805
enum AVMediaType codec_type
Definition: avcodec.h:1510
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
enum AVCodecID codec_id
Definition: avcodec.h:1519
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
void * buf
Definition: avisynth_c.h:553
#define LMLM4_INVALID
Definition: lmlm4.c:33
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
static int lmlm4_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: lmlm4.c:83
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int stream_index
Definition: avcodec.h:1425
This structure stores compressed data.
Definition: avcodec.h:1400