FFmpeg  4.4.5
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  FFRawDemuxerContext *raw = s->priv_data;
38  int ret, size;
39 
40  size = raw->raw_packet_size;
41 
42  if ((ret = av_new_packet(pkt, size)) < 0)
43  return ret;
44 
45  pkt->pos= avio_tell(s->pb);
46  pkt->stream_index = 0;
47  ret = avio_read_partial(s->pb, pkt->data, size);
48  if (ret < 0) {
50  return ret;
51  }
52  av_shrink_packet(pkt, ret);
53  return ret;
54 }
55 
57 {
59  if (!st)
60  return AVERROR(ENOMEM);
62  st->codecpar->codec_id = s->iformat->raw_codec_id;
64  st->start_time = 0;
65  /* the parameters will be extracted from the compressed bitstream */
66 
67  return 0;
68 }
69 
70 /* MPEG-1/H.263 input */
72 {
73  AVStream *st;
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 
85  st->codecpar->codec_id = s->iformat->raw_codec_id;
87 
88  st->internal->avctx->framerate = 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->codecpar->codec_id = s->iformat->raw_codec_id;
102  st->start_time = 0;
103  return 0;
104 }
105 
107 {
109  if (!st)
110  return AVERROR(ENOMEM);
112  st->codecpar->codec_id = s->iformat->raw_codec_id;
113  st->start_time = 0;
114  return 0;
115 }
116 
117 /* Note: Do not forget to add new entries to the Makefile as well. */
118 
119 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
120 #define DEC AV_OPT_FLAG_DECODING_PARAM
122  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
123  { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
124  { NULL },
125 };
126 #undef OFFSET
127 #define OFFSET(x) offsetof(FFRawDemuxerContext, x)
129  { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
130  { NULL },
131 };
132 
133 #if CONFIG_DATA_DEMUXER
134 FF_RAW_DEMUXER_CLASS(raw_data)
135 
137  .name = "data",
138  .long_name = NULL_IF_CONFIG_SMALL("raw data"),
139  .read_header = ff_raw_data_read_header,
140  .read_packet = ff_raw_read_partial_packet,
141  .raw_codec_id = AV_CODEC_ID_NONE,
142  .flags = AVFMT_NOTIMESTAMPS,
143  .priv_data_size = sizeof(FFRawDemuxerContext),\
144  .priv_class = &raw_data_demuxer_class,\
145 };
146 #endif
147 
148 #if CONFIG_MJPEG_DEMUXER
149 static int mjpeg_probe(const AVProbeData *p)
150 {
151  int i;
152  int state = -1;
153  int nb_invalid = 0;
154  int nb_frames = 0;
155 
156  for (i = 0; i < p->buf_size - 1; i++) {
157  int c;
158  if (p->buf[i] != 0xFF)
159  continue;
160  c = p->buf[i+1];
161  switch (c) {
162  case 0xD8:
163  state = 0xD8;
164  break;
165  case 0xC0:
166  case 0xC1:
167  case 0xC2:
168  case 0xC3:
169  case 0xC5:
170  case 0xC6:
171  case 0xC7:
172  case 0xF7:
173  if (state == 0xD8) {
174  state = 0xC0;
175  } else
176  nb_invalid++;
177  break;
178  case 0xDA:
179  if (state == 0xC0) {
180  state = 0xDA;
181  } else
182  nb_invalid++;
183  break;
184  case 0xD9:
185  if (state == 0xDA) {
186  state = 0xD9;
187  nb_frames++;
188  } else
189  nb_invalid++;
190  break;
191  default:
192  if ( (c >= 0x02 && c <= 0xBF)
193  || c == 0xC8) {
194  nb_invalid++;
195  }
196  }
197  }
198 
199  if (nb_invalid*4 + 1 < nb_frames) {
200  static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
201  int i;
202 
203  for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
204  if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
206 
207  if (nb_invalid == 0 && nb_frames > 2)
208  return AVPROBE_SCORE_EXTENSION / 2;
209  return AVPROBE_SCORE_EXTENSION / 4;
210  }
211  if (!nb_invalid && nb_frames)
212  return AVPROBE_SCORE_EXTENSION / 4;
213 
214  return 0;
215 }
216 
217 FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
218 #endif
AVInputFormat ff_data_demuxer
simple assert() macros that are a bit more flexible than ISO C assert().
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:798
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:704
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
#define s(width, name)
Definition: cbs_vp9.c:257
static struct @321 state
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:238
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
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
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR(e)
Definition: error.h:43
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
int i
Definition: input.c:407
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:4945
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:56
int ff_raw_data_read_header(AVFormatContext *s)
Definition: rawdec.c:106
#define RAW_PACKET_SIZE
Definition: rawdec.c:33
const AVOption ff_rawvideo_options[]
Definition: rawdec.c:121
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:35
int ff_raw_subtitle_read_header(AVFormatContext *s)
Definition: rawdec.c:95
int ff_raw_video_read_header(AVFormatContext *s)
Definition: rawdec.c:71
const AVOption ff_raw_options[]
Definition: rawdec.c:128
#define OFFSET(x)
Definition: rawdec.c:127
#define DEC
Definition: rawdec.c:120
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVOptions.
misc parsing utilities
#define FF_RAW_DEMUXER_CLASS(name)
Definition: rawdec.h:55
#define FF_DEF_RAWVIDEO_DEMUXER2(shortname, longname, probe, ext, id, flag)
Definition: rawdec.h:71
#define s1
Definition: regdef.h:38
AVRational framerate
Definition: avcodec.h:2071
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:180
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1113
int framerate
Definition: h264_levels.c:65
AVPacket * pkt
Definition: movenc.c:59
int size
static double c[64]