FFmpeg
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/intreadwrite.h"
31 
32 #define RAW_PACKET_SIZE 1024
33 
35 {
36  FFRawDemuxerContext *raw = s->priv_data;
37  int ret, size;
38 
39  size = raw->raw_packet_size;
40 
41  if ((ret = av_new_packet(pkt, size)) < 0)
42  return ret;
43 
44  pkt->pos= avio_tell(s->pb);
45  pkt->stream_index = 0;
46  ret = avio_read_partial(s->pb, pkt->data, size);
47  if (ret < 0) {
49  return ret;
50  }
52  return ret;
53 }
54 
56 {
58  if (!st)
59  return AVERROR(ENOMEM);
61  st->codecpar->codec_id = s->iformat->raw_codec_id;
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;
73  FFStream *sti;
74  FFRawVideoDemuxerContext *s1 = s->priv_data;
75  int ret = 0;
76 
77 
78  st = avformat_new_stream(s, NULL);
79  if (!st) {
80  ret = AVERROR(ENOMEM);
81  goto fail;
82  }
83  sti = ffstream(st);
84 
86  st->codecpar->codec_id = s->iformat->raw_codec_id;
88 
89  sti->avctx->framerate = s1->framerate;
90  avpriv_set_pts_info(st, 64, 1, 1200000);
91 
92 fail:
93  return ret;
94 }
95 
97 {
99  if (!st)
100  return AVERROR(ENOMEM);
102  st->codecpar->codec_id = s->iformat->raw_codec_id;
103  st->start_time = 0;
104  return 0;
105 }
106 
108 {
110  if (!st)
111  return AVERROR(ENOMEM);
113  st->codecpar->codec_id = s->iformat->raw_codec_id;
114  st->start_time = 0;
115  return 0;
116 }
117 
118 /* Note: Do not forget to add new entries to the Makefile as well. */
119 
120 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
121 #define DEC AV_OPT_FLAG_DECODING_PARAM
122 static const AVOption rawvideo_options[] = {
123  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
124  { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
125  { NULL },
126 };
127 #undef OFFSET
128 
130  .class_name = "generic raw video demuxer",
131  .item_name = av_default_item_name,
132  .option = rawvideo_options,
133  .version = LIBAVUTIL_VERSION_INT,
134 };
135 
136 #define OFFSET(x) offsetof(FFRawDemuxerContext, x)
137 static const AVOption raw_options[] = {
138  { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
139  { NULL },
140 };
141 
143  .class_name = "generic raw demuxer",
144  .item_name = av_default_item_name,
145  .option = raw_options,
146  .version = LIBAVUTIL_VERSION_INT,
147 };
148 
149 #if CONFIG_DATA_DEMUXER
151  .name = "data",
152  .long_name = NULL_IF_CONFIG_SMALL("raw data"),
153  .read_header = raw_data_read_header,
154  .read_packet = ff_raw_read_partial_packet,
155  .raw_codec_id = AV_CODEC_ID_NONE,
156  .flags = AVFMT_NOTIMESTAMPS,
157  .priv_data_size = sizeof(FFRawDemuxerContext),\
158  .priv_class = &ff_raw_demuxer_class,
159 };
160 #endif
161 
162 #if CONFIG_MJPEG_DEMUXER
163 static int mjpeg_probe(const AVProbeData *p)
164 {
165  int i;
166  int state = -1;
167  int nb_invalid = 0;
168  int nb_frames = 0;
169 
170  for (i = 0; i < p->buf_size - 1; i++) {
171  int c;
172  if (p->buf[i] != 0xFF)
173  continue;
174  c = p->buf[i+1];
175  switch (c) {
176  case 0xD8:
177  state = 0xD8;
178  break;
179  case 0xC0:
180  case 0xC1:
181  case 0xC2:
182  case 0xC3:
183  case 0xC5:
184  case 0xC6:
185  case 0xC7:
186  case 0xF7:
187  if (state == 0xD8) {
188  state = 0xC0;
189  } else
190  nb_invalid++;
191  break;
192  case 0xDA:
193  if (state == 0xC0) {
194  state = 0xDA;
195  } else
196  nb_invalid++;
197  break;
198  case 0xD9:
199  if (state == 0xDA) {
200  state = 0xD9;
201  nb_frames++;
202  } else
203  nb_invalid++;
204  break;
205  default:
206  if ( (c >= 0x02 && c <= 0xBF)
207  || c == 0xC8) {
208  nb_invalid++;
209  }
210  }
211  }
212 
213  if (nb_invalid*4 + 1 < nb_frames) {
214  static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
215  int i;
216 
217  for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
218  if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
220 
221  if (nb_invalid == 0 && nb_frames > 2)
222  return AVPROBE_SCORE_EXTENSION / 2;
223  return AVPROBE_SCORE_EXTENSION / 4;
224  }
225  if (!nb_invalid && nb_frames)
226  return AVPROBE_SCORE_EXTENSION / 4;
227 
228  return 0;
229 }
230 
231 FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
232 #endif
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:424
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_raw_video_read_header
int ff_raw_video_read_header(AVFormatContext *s)
Definition: rawdec.c:70
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ff_rawvideo_demuxer_class
const AVClass ff_rawvideo_demuxer_class
Definition: rawdec.c:129
raw_options
static const AVOption raw_options[]
Definition: rawdec.c:137
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:237
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
ff_raw_subtitle_read_header
int ff_raw_subtitle_read_header(AVFormatContext *s)
Definition: rawdec.c:96
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1710
framerate
int framerate
Definition: h264_levels.c:65
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:432
fail
#define fail()
Definition: checkasm.h:127
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
FFRawDemuxerContext
Definition: rawdec.h:36
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:476
rawvideo_options
static const AVOption rawvideo_options[]
Definition: rawdec.c:122
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:650
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
s1
#define s1
Definition: regdef.h:38
FFRawDemuxerContext::raw_packet_size
int raw_packet_size
Definition: rawdec.h:38
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
ff_data_demuxer
const AVInputFormat ff_data_demuxer
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:405
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:142
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
rawdec.h
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
parseutils.h
ff_raw_audio_read_header
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
RAW_PACKET_SIZE
#define RAW_PACKET_SIZE
Definition: rawdec.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:457
state
static struct @320 state
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
FFStream
Definition: internal.h:194
size
int size
Definition: twinvq_data.h:10344
FFRawVideoDemuxerContext
Definition: rawdec.h:28
OFFSET
#define OFFSET(x)
Definition: rawdec.c:136
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:57
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avio_internal.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
raw_data_read_header
static int raw_data_read_header(AVFormatContext *s)
Definition: rawdec.c:107
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
FFStream::avctx
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:221
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
FF_DEF_RAWVIDEO_DEMUXER2
#define FF_DEF_RAWVIDEO_DEMUXER2(shortname, longname, probe, ext, id, flag)
Definition: rawdec.h:52
AVSTREAM_PARSE_FULL_RAW
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:796
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, 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:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
DEC
#define DEC
Definition: rawdec.c:121
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:393
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:975
avio_read_partial
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:713