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/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 = avio_read_partial(s->pb, pkt->data, size);
47  if (ret < 0) {
48  av_packet_unref(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->internal->avctx->framerate = s1->framerate;
88  avpriv_set_pts_info(st, 64, 1, 1200000);
89 
90 fail:
91  return ret;
92 }
93 
95 {
97  if (!st)
98  return AVERROR(ENOMEM);
101  st->start_time = 0;
102  return 0;
103 }
104 
106 {
108  if (!st)
109  return AVERROR(ENOMEM);
112  st->start_time = 0;
113  return 0;
114 }
115 
116 /* Note: Do not forget to add new entries to the Makefile as well. */
117 
118 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
119 #define DEC AV_OPT_FLAG_DECODING_PARAM
121  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
122  { NULL },
123 };
124 
125 #if CONFIG_DATA_DEMUXER
127  .name = "data",
128  .long_name = NULL_IF_CONFIG_SMALL("raw data"),
129  .read_header = ff_raw_data_read_header,
130  .read_packet = ff_raw_read_partial_packet,
131  .raw_codec_id = AV_CODEC_ID_NONE,
132  .flags = AVFMT_NOTIMESTAMPS,
133 };
134 #endif
135 
136 #if CONFIG_MJPEG_DEMUXER
137 static int mjpeg_probe(const AVProbeData *p)
138 {
139  int i;
140  int state = -1;
141  int nb_invalid = 0;
142  int nb_frames = 0;
143 
144  for (i = 0; i < p->buf_size - 1; i++) {
145  int c;
146  if (p->buf[i] != 0xFF)
147  continue;
148  c = p->buf[i+1];
149  switch (c) {
150  case 0xD8:
151  state = 0xD8;
152  break;
153  case 0xC0:
154  case 0xC1:
155  case 0xC2:
156  case 0xC3:
157  case 0xC5:
158  case 0xC6:
159  case 0xC7:
160  case 0xF7:
161  if (state == 0xD8) {
162  state = 0xC0;
163  } else
164  nb_invalid++;
165  break;
166  case 0xDA:
167  if (state == 0xC0) {
168  state = 0xDA;
169  } else
170  nb_invalid++;
171  break;
172  case 0xD9:
173  if (state == 0xDA) {
174  state = 0xD9;
175  nb_frames++;
176  } else
177  nb_invalid++;
178  break;
179  default:
180  if ( (c >= 0x02 && c <= 0xBF)
181  || c == 0xC8) {
182  nb_invalid++;
183  }
184  }
185  }
186 
187  if (nb_invalid*4 + 1 < nb_frames) {
188  static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
189  int i;
190 
191  for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
192  if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
194 
195  if (nb_invalid == 0 && nb_frames > 2)
196  return AVPROBE_SCORE_EXTENSION / 2;
197  return AVPROBE_SCORE_EXTENSION / 4;
198  }
199 
200  return 0;
201 }
202 
203 FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
204 #endif
const AVOption ff_rawvideo_options[]
Definition: rawdec.c:120
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3101
AVOption.
Definition: opt.h:246
int ff_raw_data_read_header(AVFormatContext *s)
Definition: rawdec.c:105
AVInputFormat ff_data_demuxer
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
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:4882
#define FF_DEF_RAWVIDEO_DEMUXER2(shortname, longname, probe, ext, id, flag)
Definition: rawdec.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3953
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:717
static AVPacket pkt
int framerate
Definition: h264_levels.c:65
int ff_raw_subtitle_read_header(AVFormatContext *s)
Definition: rawdec.c:94
Format I/O context.
Definition: avformat.h:1358
Opaque data information usually continuous.
Definition: avutil.h:203
AVOptions.
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
enum AVStreamParseType need_parsing
Definition: avformat.h:1099
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
uint8_t * data
Definition: avcodec.h:1477
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:804
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
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:186
ff_const59 struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1370
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3949
simple assert() macros that are a bit more flexible than ISO C assert().
#define fail()
Definition: checkasm.h:120
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
#define FFMIN(a, b)
Definition: common.h:96
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:55
#define s(width, name)
Definition: cbs_vp9.c:257
#define RAW_PACKET_SIZE
Definition: rawdec.c:33
#define OFFSET(x)
Definition: rawdec.c:118
Stream structure.
Definition: avformat.h:881
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1238
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:468
offset must point to AVRational
Definition: opt.h:236
#define s1
Definition: regdef.h:38
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:700
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:920
AVRational framerate
AVRational describing framerate, set by a private option.
Definition: rawdec.h:33
void * priv_data
Format private data.
Definition: avformat.h:1386
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:172
#define DEC
Definition: rawdec.c:119
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
int stream_index
Definition: avcodec.h:1479
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
int ff_raw_video_read_header(AVFormatContext *s)
Definition: rawdec.c:70
This structure stores compressed data.
Definition: avcodec.h:1454
static struct @308 state