FFmpeg  4.4.5
yuv4mpegdec.c
Go to the documentation of this file.
1 /*
2  * YUV4MPEG demuxer
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/imgutils.h"
23 
24 #include "avformat.h"
25 #include "internal.h"
26 #include "yuv4mpeg.h"
27 
28 /* Header size increased to allow room for optional flags */
29 #define MAX_YUV4_HEADER 96
30 #define MAX_FRAME_HEADER 80
31 
33 {
34  char header[MAX_YUV4_HEADER + 10]; // Include headroom for
35  // the longest option
36  char *tokstart, *tokend, *header_end;
37  int i;
38  AVIOContext *pb = s->pb;
39  int width = -1, height = -1, raten = 0,
40  rated = 0, aspectn = 0, aspectd = 0;
42  enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
43  enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
45  AVStream *st;
46 
47  for (i = 0; i < MAX_YUV4_HEADER; i++) {
48  header[i] = avio_r8(pb);
49  if (header[i] == '\n') {
50  header[i + 1] = 0x20; // Add a space after last option.
51  // Makes parsing "444" vs "444alpha" easier.
52  header[i + 2] = 0;
53  break;
54  }
55  }
56  if (i == MAX_YUV4_HEADER) {
57  av_log(s, AV_LOG_ERROR, "Header too large.\n");
58  return AVERROR(EINVAL);
59  }
60  if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) {
61  av_log(s, AV_LOG_ERROR, "Invalid magic number for yuv4mpeg.\n");
62  return AVERROR(EINVAL);
63  }
64 
65  header_end = &header[i + 1]; // Include space
66  for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
67  tokstart < header_end; tokstart++) {
68  if (*tokstart == 0x20)
69  continue;
70  switch (*tokstart++) {
71  case 'W': // Width. Required.
72  width = strtol(tokstart, &tokend, 10);
73  tokstart = tokend;
74  break;
75  case 'H': // Height. Required.
76  height = strtol(tokstart, &tokend, 10);
77  tokstart = tokend;
78  break;
79  case 'C': // Color space
80  if (strncmp("420jpeg", tokstart, 7) == 0) {
82  chroma_sample_location = AVCHROMA_LOC_CENTER;
83  } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
85  chroma_sample_location = AVCHROMA_LOC_LEFT;
86  } else if (strncmp("420paldv", tokstart, 8) == 0) {
88  chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
89  } else if (strncmp("420p16", tokstart, 6) == 0) {
91  } else if (strncmp("422p16", tokstart, 6) == 0) {
93  } else if (strncmp("444p16", tokstart, 6) == 0) {
95  } else if (strncmp("420p14", tokstart, 6) == 0) {
97  } else if (strncmp("422p14", tokstart, 6) == 0) {
99  } else if (strncmp("444p14", tokstart, 6) == 0) {
101  } else if (strncmp("420p12", tokstart, 6) == 0) {
103  } else if (strncmp("422p12", tokstart, 6) == 0) {
105  } else if (strncmp("444p12", tokstart, 6) == 0) {
107  } else if (strncmp("420p10", tokstart, 6) == 0) {
109  } else if (strncmp("422p10", tokstart, 6) == 0) {
111  } else if (strncmp("444p10", tokstart, 6) == 0) {
113  } else if (strncmp("420p9", tokstart, 5) == 0) {
115  } else if (strncmp("422p9", tokstart, 5) == 0) {
117  } else if (strncmp("444p9", tokstart, 5) == 0) {
119  } else if (strncmp("420", tokstart, 3) == 0) {
121  chroma_sample_location = AVCHROMA_LOC_CENTER;
122  } else if (strncmp("411", tokstart, 3) == 0) {
124  } else if (strncmp("422", tokstart, 3) == 0) {
126  } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
128  } else if (strncmp("444", tokstart, 3) == 0) {
130  } else if (strncmp("mono16", tokstart, 6) == 0) {
132  } else if (strncmp("mono12", tokstart, 6) == 0) {
134  } else if (strncmp("mono10", tokstart, 6) == 0) {
136  } else if (strncmp("mono9", tokstart, 5) == 0) {
138  } else if (strncmp("mono", tokstart, 4) == 0) {
140  } else {
141  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
142  "pixel format.\n");
143  return AVERROR_INVALIDDATA;
144  }
145  while (tokstart < header_end && *tokstart != 0x20)
146  tokstart++;
147  break;
148  case 'I': // Interlace type
149  switch (*tokstart++){
150  case '?':
151  field_order = AV_FIELD_UNKNOWN;
152  break;
153  case 'p':
154  field_order = AV_FIELD_PROGRESSIVE;
155  break;
156  case 't':
157  field_order = AV_FIELD_TT;
158  break;
159  case 'b':
160  field_order = AV_FIELD_BB;
161  break;
162  case 'm':
163  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
164  "interlaced and non-interlaced frames.\n");
165  default:
166  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
167  return AVERROR(EINVAL);
168  }
169  break;
170  case 'F': // Frame rate
171  sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
172  while (tokstart < header_end && *tokstart != 0x20)
173  tokstart++;
174  break;
175  case 'A': // Pixel aspect
176  sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
177  while (tokstart < header_end && *tokstart != 0x20)
178  tokstart++;
179  break;
180  case 'X': // Vendor extensions
181  if (strncmp("YSCSS=", tokstart, 6) == 0) {
182  // Older nonstandard pixel format representation
183  tokstart += 6;
184  if (strncmp("420JPEG", tokstart, 7) == 0)
185  alt_pix_fmt = AV_PIX_FMT_YUV420P;
186  else if (strncmp("420MPEG2", tokstart, 8) == 0)
187  alt_pix_fmt = AV_PIX_FMT_YUV420P;
188  else if (strncmp("420PALDV", tokstart, 8) == 0)
189  alt_pix_fmt = AV_PIX_FMT_YUV420P;
190  else if (strncmp("420P9", tokstart, 5) == 0)
191  alt_pix_fmt = AV_PIX_FMT_YUV420P9;
192  else if (strncmp("422P9", tokstart, 5) == 0)
193  alt_pix_fmt = AV_PIX_FMT_YUV422P9;
194  else if (strncmp("444P9", tokstart, 5) == 0)
195  alt_pix_fmt = AV_PIX_FMT_YUV444P9;
196  else if (strncmp("420P10", tokstart, 6) == 0)
197  alt_pix_fmt = AV_PIX_FMT_YUV420P10;
198  else if (strncmp("422P10", tokstart, 6) == 0)
199  alt_pix_fmt = AV_PIX_FMT_YUV422P10;
200  else if (strncmp("444P10", tokstart, 6) == 0)
201  alt_pix_fmt = AV_PIX_FMT_YUV444P10;
202  else if (strncmp("420P12", tokstart, 6) == 0)
203  alt_pix_fmt = AV_PIX_FMT_YUV420P12;
204  else if (strncmp("422P12", tokstart, 6) == 0)
205  alt_pix_fmt = AV_PIX_FMT_YUV422P12;
206  else if (strncmp("444P12", tokstart, 6) == 0)
207  alt_pix_fmt = AV_PIX_FMT_YUV444P12;
208  else if (strncmp("420P14", tokstart, 6) == 0)
209  alt_pix_fmt = AV_PIX_FMT_YUV420P14;
210  else if (strncmp("422P14", tokstart, 6) == 0)
211  alt_pix_fmt = AV_PIX_FMT_YUV422P14;
212  else if (strncmp("444P14", tokstart, 6) == 0)
213  alt_pix_fmt = AV_PIX_FMT_YUV444P14;
214  else if (strncmp("420P16", tokstart, 6) == 0)
215  alt_pix_fmt = AV_PIX_FMT_YUV420P16;
216  else if (strncmp("422P16", tokstart, 6) == 0)
217  alt_pix_fmt = AV_PIX_FMT_YUV422P16;
218  else if (strncmp("444P16", tokstart, 6) == 0)
219  alt_pix_fmt = AV_PIX_FMT_YUV444P16;
220  else if (strncmp("411", tokstart, 3) == 0)
221  alt_pix_fmt = AV_PIX_FMT_YUV411P;
222  else if (strncmp("422", tokstart, 3) == 0)
223  alt_pix_fmt = AV_PIX_FMT_YUV422P;
224  else if (strncmp("444", tokstart, 3) == 0)
225  alt_pix_fmt = AV_PIX_FMT_YUV444P;
226  } else if (strncmp("COLORRANGE=", tokstart, 11) == 0) {
227  tokstart += 11;
228  if (strncmp("FULL",tokstart, 4) == 0)
230  else if (strncmp("LIMITED", tokstart, 7) == 0)
232  }
233  while (tokstart < header_end && *tokstart != 0x20)
234  tokstart++;
235  break;
236  }
237  }
238 
239  if (width == -1 || height == -1) {
240  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
241  return AVERROR_INVALIDDATA;
242  }
243 
244  if (pix_fmt == AV_PIX_FMT_NONE) {
245  if (alt_pix_fmt == AV_PIX_FMT_NONE)
247  else
248  pix_fmt = alt_pix_fmt;
249  }
250 
251  if (raten <= 0 || rated <= 0) {
252  // Frame rate unknown
253  raten = 25;
254  rated = 1;
255  }
256 
257  if (aspectn == 0 && aspectd == 0) {
258  // Pixel aspect unknown
259  aspectd = 1;
260  }
261 
262  st = avformat_new_stream(s, NULL);
263  if (!st)
264  return AVERROR(ENOMEM);
265  st->codecpar->width = width;
266  st->codecpar->height = height;
267  av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
268  avpriv_set_pts_info(st, 64, rated, raten);
269  st->avg_frame_rate = av_inv_q(st->time_base);
270  st->codecpar->format = pix_fmt;
273  st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
274  st->codecpar->chroma_location = chroma_sample_location;
276  st->codecpar->field_order = field_order;
278  if ((int) s->packet_size < 0)
279  return s->packet_size;
280  s->internal->data_offset = avio_tell(pb);
281 
282  st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
283 
284  return 0;
285 }
286 
288 {
289  int i;
290  char header[MAX_FRAME_HEADER+1];
291  int ret;
292  int64_t off = avio_tell(s->pb);
293 
294  for (i = 0; i < MAX_FRAME_HEADER; i++) {
295  header[i] = avio_r8(s->pb);
296  if (header[i] == '\n') {
297  header[i + 1] = 0;
298  break;
299  }
300  }
301  if (s->pb->error)
302  return s->pb->error;
303  else if (s->pb->eof_reached)
304  return AVERROR_EOF;
305  else if (i == MAX_FRAME_HEADER)
306  return AVERROR_INVALIDDATA;
307 
308  if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
309  return AVERROR_INVALIDDATA;
310 
311  ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
312  if (ret < 0)
313  return ret;
314  else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
315  return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
316  }
317  pkt->stream_index = 0;
318  pkt->pts = (off - s->internal->data_offset) / s->packet_size;
319  pkt->duration = 1;
320  return 0;
321 }
322 
323 static int yuv4_read_seek(AVFormatContext *s, int stream_index,
324  int64_t pts, int flags)
325 {
326  int64_t pos;
327 
329  pts = FFMAX(0, pts - 1);
330  if (pts < 0)
331  return -1;
332  pos = pts * s->packet_size;
333 
334  if (avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET) < 0)
335  return -1;
336  return 0;
337 }
338 
339 static int yuv4_probe(const AVProbeData *pd)
340 {
341  /* check file header */
342  if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
343  return AVPROBE_SCORE_MAX;
344  else
345  return 0;
346 }
347 
349  .name = "yuv4mpegpipe",
350  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
351  .read_probe = yuv4_probe,
352  .read_header = yuv4_read_header,
353  .read_packet = yuv4_read_packet,
354  .read_seek = yuv4_read_seek,
355  .extensions = "y4m",
356 };
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
AVFieldOrder
Definition: codec_par.h:36
@ AV_FIELD_TT
Definition: codec_par.h:39
@ AV_FIELD_BB
Definition: codec_par.h:40
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:37
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:38
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static enum AVPixelFormat pix_fmt
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:62
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition: imgutils.c:466
misc image utilities
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
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
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:605
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:609
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:607
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:608
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:606
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
AVColorRange
Visual content value range.
Definition: pixfmt.h:551
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:552
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
static const uint8_t header[24]
Definition: sdr2.c:67
unsigned int pos
Definition: spdifenc.c:412
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
int width
Video only.
Definition: codec_par.h:126
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
enum AVChromaLocation chroma_location
Definition: codec_par.h:150
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
static int64_t pts
color_range
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
#define Y4M_FRAME_MAGIC_LEN
Definition: yuv4mpeg.h:26
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
AVInputFormat ff_yuv4mpegpipe_demuxer
Definition: yuv4mpegdec.c:348
static int yuv4_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: yuv4mpegdec.c:323
static int yuv4_read_header(AVFormatContext *s)
Definition: yuv4mpegdec.c:32
static int yuv4_probe(const AVProbeData *pd)
Definition: yuv4mpegdec.c:339
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegdec.c:287
#define MAX_YUV4_HEADER
Definition: yuv4mpegdec.c:29
#define MAX_FRAME_HEADER
Definition: yuv4mpegdec.c:30