FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/avassert.h"
31 #include "libavutil/intreadwrite.h"
32 
33 #define RAW_PACKET_SIZE 1024
34 
36 {
37  int ret, size;
38 
39  size = RAW_PACKET_SIZE;
40 
41  if (av_new_packet(pkt, size) < 0)
42  return AVERROR(ENOMEM);
43 
44  pkt->pos= avio_tell(s->pb);
45  pkt->stream_index = 0;
46  ret = ffio_read_partial(s->pb, pkt->data, size);
47  if (ret < 0) {
48  av_free_packet(pkt);
49  return ret;
50  }
51  av_shrink_packet(pkt, ret);
52  return ret;
53 }
54 
56 {
58  if (!st)
59  return AVERROR(ENOMEM);
63  st->start_time = 0;
64  /* the parameters will be extracted from the compressed bitstream */
65 
66  return 0;
67 }
68 
69 /* MPEG-1/H.263 input */
71 {
72  AVStream *st;
74  int ret = 0;
75 
76 
77  st = avformat_new_stream(s, NULL);
78  if (!st) {
79  ret = AVERROR(ENOMEM);
80  goto fail;
81  }
82 
86 
87  st->codec->framerate = s1->framerate;
88  st->codec->time_base = av_inv_q(s1->framerate);
89  avpriv_set_pts_info(st, 64, 1, 1200000);
90 
91 fail:
92  return ret;
93 }
94 
96 {
98  if (!st)
99  return AVERROR(ENOMEM);
101  st->codec->codec_id = s->iformat->raw_codec_id;
102  st->start_time = 0;
103  return 0;
104 }
105 
106 /* Note: Do not forget to add new entries to the Makefile as well. */
107 
108 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
109 #define DEC AV_OPT_FLAG_DECODING_PARAM
111  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC},
112  { NULL },
113 };
114 
115 #if CONFIG_DATA_DEMUXER
116 AVInputFormat ff_data_demuxer = {
117  .name = "data",
118  .long_name = NULL_IF_CONFIG_SMALL("raw data"),
119  .read_header = ff_raw_data_read_header,
120  .read_packet = ff_raw_read_partial_packet,
121  .raw_codec_id = AV_CODEC_ID_NONE,
122  .flags = AVFMT_NOTIMESTAMPS,
123 };
124 #endif
125 
126 #if CONFIG_LATM_DEMUXER
127 
128 AVInputFormat ff_latm_demuxer = {
129  .name = "latm",
130  .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
131  .read_header = ff_raw_audio_read_header,
132  .read_packet = ff_raw_read_partial_packet,
134  .extensions = "latm",
135  .raw_codec_id = AV_CODEC_ID_AAC_LATM,
136 };
137 #endif
138 
139 #if CONFIG_MJPEG_DEMUXER
140 static int mjpeg_probe(AVProbeData *p)
141 {
142  int i;
143  int state = -1;
144  int nb_invalid = 0;
145  int nb_frames = 0;
146 
147  for (i=0; i<p->buf_size-2; i++) {
148  int c;
149  if (p->buf[i] != 0xFF)
150  continue;
151  c = p->buf[i+1];
152  switch (c) {
153  case 0xD8:
154  state = 0xD8;
155  break;
156  case 0xC0:
157  case 0xC1:
158  case 0xC2:
159  case 0xC3:
160  case 0xC5:
161  case 0xC6:
162  case 0xC7:
163  case 0xF7:
164  if (state == 0xD8) {
165  state = 0xC0;
166  } else
167  nb_invalid++;
168  break;
169  case 0xDA:
170  if (state == 0xC0) {
171  state = 0xDA;
172  } else
173  nb_invalid++;
174  break;
175  case 0xD9:
176  if (state == 0xDA) {
177  state = 0xD9;
178  nb_frames++;
179  } else
180  nb_invalid++;
181  break;
182  default:
183  if ( (c >= 0x02 && c <= 0xBF)
184  || c == 0xC8) {
185  nb_invalid++;
186  }
187  }
188  }
189 
190  if (nb_invalid*4 + 1 < nb_frames) {
191  static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n\r\n";
192  int i;
193 
194  for (i=0; i<FFMIN(p->buf_size - sizeof(ct_jpeg), 100); i++)
195  if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
197 
198  if (nb_invalid == 0 && nb_frames > 2)
199  return AVPROBE_SCORE_EXTENSION / 2;
200  return AVPROBE_SCORE_EXTENSION / 4;
201  }
202 
203  return 0;
204 }
205 
206 FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
207 #endif
208 
209 #if CONFIG_MLP_DEMUXER
210 AVInputFormat ff_mlp_demuxer = {
211  .name = "mlp",
212  .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
213  .read_header = ff_raw_audio_read_header,
214  .read_packet = ff_raw_read_partial_packet,
216  .extensions = "mlp",
217  .raw_codec_id = AV_CODEC_ID_MLP,
218 };
219 #endif
220 
221 #if CONFIG_TRUEHD_DEMUXER
222 AVInputFormat ff_truehd_demuxer = {
223  .name = "truehd",
224  .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
225  .read_header = ff_raw_audio_read_header,
226  .read_packet = ff_raw_read_partial_packet,
228  .extensions = "thd",
229  .raw_codec_id = AV_CODEC_ID_TRUEHD,
230 };
231 #endif
232 
233 #if CONFIG_SHORTEN_DEMUXER
234 AVInputFormat ff_shorten_demuxer = {
235  .name = "shn",
236  .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
237  .read_header = ff_raw_audio_read_header,
238  .read_packet = ff_raw_read_partial_packet,
240  .extensions = "shn",
241  .raw_codec_id = AV_CODEC_ID_SHORTEN,
242 };
243 #endif
244 
245 #if CONFIG_VC1_DEMUXER
247 #endif
const AVOption ff_rawvideo_options[]
Definition: rawdec.c:110
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3023
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:478
const char * s
Definition: avisynth_c.h:631
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
AVOption.
Definition: opt.h:255
int ff_raw_data_read_header(AVFormatContext *s)
Definition: rawdec.c:95
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1187
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
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:4006
#define FF_DEF_RAWVIDEO_DEMUXER2(shortname, longname, probe, ext, id, flag)
Definition: rawdec.h:54
static AVPacket pkt
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1369
Format I/O context.
Definition: avformat.h:1272
Opaque data information usually continuous.
Definition: avutil.h:196
AVOptions.
enum AVStreamParseType need_parsing
Definition: avformat.h:1035
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3672
uint8_t * data
Definition: avcodec.h:1162
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:365
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:778
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
#define AVERROR(e)
Definition: error.h:43
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:35
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
simple assert() macros that are a bit more flexible than ISO C assert().
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:605
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
#define FFMIN(a, b)
Definition: common.h:66
ret
Definition: avfilter.c:974
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:55
#define RAW_PACKET_SIZE
Definition: rawdec.c:33
#define OFFSET(x)
Definition: rawdec.c:108
Stream structure.
Definition: avformat.h:842
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:472
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:479
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:473
offset must point to AVRational
Definition: opt.h:235
#define s1
Definition: regdef.h:38
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:674
static uint32_t state
Definition: trasher.c:27
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:894
static double c[64]
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1284
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:480
AVRational framerate
AVRational describing framerate, set by a private option.
Definition: rawdec.h:33
void * priv_data
Format private data.
Definition: avformat.h:1300
#define DEC
Definition: rawdec.c:109
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int stream_index
Definition: avcodec.h:1164
int ff_raw_video_read_header(AVFormatContext *s)
Definition: rawdec.c:70
This structure stores compressed data.
Definition: avcodec.h:1139