Libav
rawdec.c
Go to the documentation of this file.
1 /*
2  * RAW demuxers
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2005 Alex Beregszaszi
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avformat.h"
24 #include "internal.h"
25 #include "avio_internal.h"
26 #include "rawdec.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/intreadwrite.h"
31 
32 #define RAW_PACKET_SIZE 1024
33 
35 {
36  int ret, size;
37 
38  size = RAW_PACKET_SIZE;
39 
40  if (av_new_packet(pkt, size) < 0)
41  return AVERROR(ENOMEM);
42 
43  pkt->pos= avio_tell(s->pb);
44  pkt->stream_index = 0;
45  ret = ffio_read_partial(s->pb, pkt->data, size);
46  if (ret < 0) {
47  av_free_packet(pkt);
48  return ret;
49  } else if (ret < size) {
50  /* initialize end of packet for partial reads to avoid reading
51  * uninitialized data on allowed overreads */
52  memset(pkt->data + ret, 0, FF_INPUT_BUFFER_PADDING_SIZE);
53  }
54  pkt->size = ret;
55  return ret;
56 }
57 
59 {
61  if (!st)
62  return AVERROR(ENOMEM);
66  st->start_time = 0;
67  /* the parameters will be extracted from the compressed bitstream */
68 
69  return 0;
70 }
71 
72 /* MPEG-1/H.263 input */
74 {
75  AVStream *st;
77  AVRational framerate;
78  int ret = 0;
79 
80 
81  st = avformat_new_stream(s, NULL);
82  if (!st) {
83  ret = AVERROR(ENOMEM);
84  goto fail;
85  }
86 
90 
91  if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
92  av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
93  goto fail;
94  }
95 
96  st->avg_frame_rate = framerate;
97  avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
98 
99 fail:
100  return ret;
101 }
102 
103 /* Note: Do not forget to add new entries to the Makefile as well. */
104 
105 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
106 #define DEC AV_OPT_FLAG_DECODING_PARAM
108  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
109  { NULL },
110 };
111 
112 #if CONFIG_LATM_DEMUXER
113 
114 #define LOAS_SYNC_WORD 0x2b7
115 
116 static int latm_read_probe(AVProbeData *p)
117 {
118  int max_frames = 0, first_frames = 0;
119  int fsize, frames;
120  uint8_t *buf0 = p->buf;
121  uint8_t *buf2;
122  uint8_t *buf;
123  uint8_t *end = buf0 + p->buf_size - 3;
124 
125  buf = buf0;
126 
127  for (; buf < end; buf = buf2 + 1) {
128  buf2 = buf;
129 
130  for (frames = 0; buf2 < end; frames++) {
131  uint32_t header = AV_RB24(buf2);
132  if ((header >> 13) != LOAS_SYNC_WORD) {
133  if (buf != buf0) {
134  // Found something that isn't a LOAS header, starting
135  // from a position other than the start of the buffer.
136  // Discard the count we've accumulated so far since it
137  // probably was a false positive.
138  frames = 0;
139  }
140  break;
141  }
142  fsize = (header & 0x1FFF) + 3;
143  if (fsize < 7)
144  break;
145  buf2 += fsize;
146  }
147  max_frames = FFMAX(max_frames, frames);
148  if (buf == buf0)
149  first_frames = frames;
150  }
151 
152  if (first_frames >= 3)
153  return AVPROBE_SCORE_EXTENSION + 1;
154  else if (max_frames > 100)
156  else if (max_frames >= 3)
157  return AVPROBE_SCORE_EXTENSION / 2;
158  else if (max_frames > 1)
159  return 1;
160  else
161  return 0;
162 }
163 
164 AVInputFormat ff_latm_demuxer = {
165  .name = "latm",
166  .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
167  .read_probe = latm_read_probe,
168  .read_header = ff_raw_audio_read_header,
169  .read_packet = ff_raw_read_partial_packet,
170  .flags = AVFMT_GENERIC_INDEX,
171  .extensions = "latm",
172  .raw_codec_id = AV_CODEC_ID_AAC_LATM,
173 };
174 #endif
175 
176 #if CONFIG_MJPEG_DEMUXER
177 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", AV_CODEC_ID_MJPEG)
178 #endif
179 
180 #if CONFIG_MLP_DEMUXER
181 AVInputFormat ff_mlp_demuxer = {
182  .name = "mlp",
183  .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
184  .read_header = ff_raw_audio_read_header,
185  .read_packet = ff_raw_read_partial_packet,
186  .flags = AVFMT_GENERIC_INDEX,
187  .extensions = "mlp",
188  .raw_codec_id = AV_CODEC_ID_MLP,
189 };
190 #endif
191 
192 #if CONFIG_TRUEHD_DEMUXER
193 AVInputFormat ff_truehd_demuxer = {
194  .name = "truehd",
195  .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
196  .read_header = ff_raw_audio_read_header,
197  .read_packet = ff_raw_read_partial_packet,
198  .flags = AVFMT_GENERIC_INDEX,
199  .extensions = "thd",
200  .raw_codec_id = AV_CODEC_ID_TRUEHD,
201 };
202 #endif
203 
204 #if CONFIG_SHORTEN_DEMUXER
205 AVInputFormat ff_shorten_demuxer = {
206  .name = "shn",
207  .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
208  .read_header = ff_raw_audio_read_header,
209  .read_packet = ff_raw_read_partial_packet,
211  .extensions = "shn",
212  .raw_codec_id = AV_CODEC_ID_SHORTEN,
213 };
214 #endif
215 
216 #if CONFIG_VC1_DEMUXER
217 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
218 #endif
const AVOption ff_rawvideo_options[]
Definition: rawdec.c:107
char * framerate
String describing framerate, set by a private option.
Definition: rawdec.h:33
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:422
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
AVOption.
Definition: opt.h:234
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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:2829
#define FF_DEF_RAWVIDEO_DEMUXER(shortname, longname, probe, ext, id)
Definition: rawdec.h:52
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
#define AV_RB24
Definition: intreadwrite.h:64
Format I/O context.
Definition: avformat.h:922
uint8_t
AVOptions.
enum AVStreamParseType need_parsing
Definition: avformat.h:867
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * data
Definition: avcodec.h:973
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define LOAS_SYNC_WORD
11 bits LOAS sync word
Definition: aacdec.c:2993
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:511
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:780
#define FFMAX(a, b)
Definition: common.h:55
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:398
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:58
#define RAW_PACKET_SIZE
Definition: rawdec.c:32
#define OFFSET(x)
Definition: rawdec.c:105
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:423
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:417
rational number numerator/denominator
Definition: rational.h:43
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:571
full parsing and repack
Definition: avformat.h:653
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
int den
denominator
Definition: rational.h:45
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:934
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:424
void * priv_data
Format private data.
Definition: avformat.h:950
#define DEC
Definition: rawdec.c:106
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
int ff_raw_video_read_header(AVFormatContext *s)
Definition: rawdec.c:73
This structure stores compressed data.
Definition: avcodec.h:950