FFmpeg  4.0
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 80
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;
44  AVStream *st;
45 
46  for (i = 0; i < MAX_YUV4_HEADER; i++) {
47  header[i] = avio_r8(pb);
48  if (header[i] == '\n') {
49  header[i + 1] = 0x20; // Add a space after last option.
50  // Makes parsing "444" vs "444alpha" easier.
51  header[i + 2] = 0;
52  break;
53  }
54  }
55  if (i == MAX_YUV4_HEADER)
56  return -1;
57  if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
58  return -1;
59 
60  header_end = &header[i + 1]; // Include space
61  for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
62  tokstart < header_end; tokstart++) {
63  if (*tokstart == 0x20)
64  continue;
65  switch (*tokstart++) {
66  case 'W': // Width. Required.
67  width = strtol(tokstart, &tokend, 10);
68  tokstart = tokend;
69  break;
70  case 'H': // Height. Required.
71  height = strtol(tokstart, &tokend, 10);
72  tokstart = tokend;
73  break;
74  case 'C': // Color space
75  if (strncmp("420jpeg", tokstart, 7) == 0) {
76  pix_fmt = AV_PIX_FMT_YUV420P;
77  chroma_sample_location = AVCHROMA_LOC_CENTER;
78  } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
79  pix_fmt = AV_PIX_FMT_YUV420P;
80  chroma_sample_location = AVCHROMA_LOC_LEFT;
81  } else if (strncmp("420paldv", tokstart, 8) == 0) {
82  pix_fmt = AV_PIX_FMT_YUV420P;
83  chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
84  } else if (strncmp("420p16", tokstart, 6) == 0) {
85  pix_fmt = AV_PIX_FMT_YUV420P16;
86  } else if (strncmp("422p16", tokstart, 6) == 0) {
87  pix_fmt = AV_PIX_FMT_YUV422P16;
88  } else if (strncmp("444p16", tokstart, 6) == 0) {
89  pix_fmt = AV_PIX_FMT_YUV444P16;
90  } else if (strncmp("420p14", tokstart, 6) == 0) {
91  pix_fmt = AV_PIX_FMT_YUV420P14;
92  } else if (strncmp("422p14", tokstart, 6) == 0) {
93  pix_fmt = AV_PIX_FMT_YUV422P14;
94  } else if (strncmp("444p14", tokstart, 6) == 0) {
95  pix_fmt = AV_PIX_FMT_YUV444P14;
96  } else if (strncmp("420p12", tokstart, 6) == 0) {
97  pix_fmt = AV_PIX_FMT_YUV420P12;
98  } else if (strncmp("422p12", tokstart, 6) == 0) {
99  pix_fmt = AV_PIX_FMT_YUV422P12;
100  } else if (strncmp("444p12", tokstart, 6) == 0) {
101  pix_fmt = AV_PIX_FMT_YUV444P12;
102  } else if (strncmp("420p10", tokstart, 6) == 0) {
103  pix_fmt = AV_PIX_FMT_YUV420P10;
104  } else if (strncmp("422p10", tokstart, 6) == 0) {
105  pix_fmt = AV_PIX_FMT_YUV422P10;
106  } else if (strncmp("444p10", tokstart, 6) == 0) {
107  pix_fmt = AV_PIX_FMT_YUV444P10;
108  } else if (strncmp("420p9", tokstart, 5) == 0) {
109  pix_fmt = AV_PIX_FMT_YUV420P9;
110  } else if (strncmp("422p9", tokstart, 5) == 0) {
111  pix_fmt = AV_PIX_FMT_YUV422P9;
112  } else if (strncmp("444p9", tokstart, 5) == 0) {
113  pix_fmt = AV_PIX_FMT_YUV444P9;
114  } else if (strncmp("420", tokstart, 3) == 0) {
115  pix_fmt = AV_PIX_FMT_YUV420P;
116  chroma_sample_location = AVCHROMA_LOC_CENTER;
117  } else if (strncmp("411", tokstart, 3) == 0) {
118  pix_fmt = AV_PIX_FMT_YUV411P;
119  } else if (strncmp("422", tokstart, 3) == 0) {
120  pix_fmt = AV_PIX_FMT_YUV422P;
121  } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
122  av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
123  "YUV4MPEG stream.\n");
124  return -1;
125  } else if (strncmp("444", tokstart, 3) == 0) {
126  pix_fmt = AV_PIX_FMT_YUV444P;
127  } else if (strncmp("mono16", tokstart, 6) == 0) {
128  pix_fmt = AV_PIX_FMT_GRAY16;
129  } else if (strncmp("mono12", tokstart, 6) == 0) {
130  pix_fmt = AV_PIX_FMT_GRAY12;
131  } else if (strncmp("mono10", tokstart, 6) == 0) {
132  pix_fmt = AV_PIX_FMT_GRAY10;
133  } else if (strncmp("mono9", tokstart, 5) == 0) {
134  pix_fmt = AV_PIX_FMT_GRAY9;
135  } else if (strncmp("mono", tokstart, 4) == 0) {
136  pix_fmt = AV_PIX_FMT_GRAY8;
137  } else {
138  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
139  "pixel format.\n");
140  return -1;
141  }
142  while (tokstart < header_end && *tokstart != 0x20)
143  tokstart++;
144  break;
145  case 'I': // Interlace type
146  switch (*tokstart++){
147  case '?':
148  field_order = AV_FIELD_UNKNOWN;
149  break;
150  case 'p':
151  field_order = AV_FIELD_PROGRESSIVE;
152  break;
153  case 't':
154  field_order = AV_FIELD_TT;
155  break;
156  case 'b':
157  field_order = AV_FIELD_BB;
158  break;
159  case 'm':
160  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
161  "interlaced and non-interlaced frames.\n");
162  default:
163  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
164  return AVERROR(EINVAL);
165  }
166  break;
167  case 'F': // Frame rate
168  sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
169  while (tokstart < header_end && *tokstart != 0x20)
170  tokstart++;
171  break;
172  case 'A': // Pixel aspect
173  sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
174  while (tokstart < header_end && *tokstart != 0x20)
175  tokstart++;
176  break;
177  case 'X': // Vendor extensions
178  if (strncmp("YSCSS=", tokstart, 6) == 0) {
179  // Older nonstandard pixel format representation
180  tokstart += 6;
181  if (strncmp("420JPEG", tokstart, 7) == 0)
182  alt_pix_fmt = AV_PIX_FMT_YUV420P;
183  else if (strncmp("420MPEG2", tokstart, 8) == 0)
184  alt_pix_fmt = AV_PIX_FMT_YUV420P;
185  else if (strncmp("420PALDV", tokstart, 8) == 0)
186  alt_pix_fmt = AV_PIX_FMT_YUV420P;
187  else if (strncmp("420P9", tokstart, 5) == 0)
188  alt_pix_fmt = AV_PIX_FMT_YUV420P9;
189  else if (strncmp("422P9", tokstart, 5) == 0)
190  alt_pix_fmt = AV_PIX_FMT_YUV422P9;
191  else if (strncmp("444P9", tokstart, 5) == 0)
192  alt_pix_fmt = AV_PIX_FMT_YUV444P9;
193  else if (strncmp("420P10", tokstart, 6) == 0)
194  alt_pix_fmt = AV_PIX_FMT_YUV420P10;
195  else if (strncmp("422P10", tokstart, 6) == 0)
196  alt_pix_fmt = AV_PIX_FMT_YUV422P10;
197  else if (strncmp("444P10", tokstart, 6) == 0)
198  alt_pix_fmt = AV_PIX_FMT_YUV444P10;
199  else if (strncmp("420P12", tokstart, 6) == 0)
200  alt_pix_fmt = AV_PIX_FMT_YUV420P12;
201  else if (strncmp("422P12", tokstart, 6) == 0)
202  alt_pix_fmt = AV_PIX_FMT_YUV422P12;
203  else if (strncmp("444P12", tokstart, 6) == 0)
204  alt_pix_fmt = AV_PIX_FMT_YUV444P12;
205  else if (strncmp("420P14", tokstart, 6) == 0)
206  alt_pix_fmt = AV_PIX_FMT_YUV420P14;
207  else if (strncmp("422P14", tokstart, 6) == 0)
208  alt_pix_fmt = AV_PIX_FMT_YUV422P14;
209  else if (strncmp("444P14", tokstart, 6) == 0)
210  alt_pix_fmt = AV_PIX_FMT_YUV444P14;
211  else if (strncmp("420P16", tokstart, 6) == 0)
212  alt_pix_fmt = AV_PIX_FMT_YUV420P16;
213  else if (strncmp("422P16", tokstart, 6) == 0)
214  alt_pix_fmt = AV_PIX_FMT_YUV422P16;
215  else if (strncmp("444P16", tokstart, 6) == 0)
216  alt_pix_fmt = AV_PIX_FMT_YUV444P16;
217  else if (strncmp("411", tokstart, 3) == 0)
218  alt_pix_fmt = AV_PIX_FMT_YUV411P;
219  else if (strncmp("422", tokstart, 3) == 0)
220  alt_pix_fmt = AV_PIX_FMT_YUV422P;
221  else if (strncmp("444", tokstart, 3) == 0)
222  alt_pix_fmt = AV_PIX_FMT_YUV444P;
223  }
224  while (tokstart < header_end && *tokstart != 0x20)
225  tokstart++;
226  break;
227  }
228  }
229 
230  if (width == -1 || height == -1) {
231  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
232  return -1;
233  }
234 
235  if (pix_fmt == AV_PIX_FMT_NONE) {
236  if (alt_pix_fmt == AV_PIX_FMT_NONE)
237  pix_fmt = AV_PIX_FMT_YUV420P;
238  else
239  pix_fmt = alt_pix_fmt;
240  }
241 
242  if (raten <= 0 || rated <= 0) {
243  // Frame rate unknown
244  raten = 25;
245  rated = 1;
246  }
247 
248  if (aspectn == 0 && aspectd == 0) {
249  // Pixel aspect unknown
250  aspectd = 1;
251  }
252 
253  st = avformat_new_stream(s, NULL);
254  if (!st)
255  return AVERROR(ENOMEM);
256  st->codecpar->width = width;
257  st->codecpar->height = height;
258  av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
259  avpriv_set_pts_info(st, 64, rated, raten);
260  st->avg_frame_rate = av_inv_q(st->time_base);
261  st->codecpar->format = pix_fmt;
264  st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
265  st->codecpar->chroma_location = chroma_sample_location;
266  st->codecpar->field_order = field_order;
268  if ((int) s->packet_size < 0)
269  return s->packet_size;
270  s->internal->data_offset = avio_tell(pb);
271 
272  st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
273 
274  return 0;
275 }
276 
278 {
279  int i;
280  char header[MAX_FRAME_HEADER+1];
281  int ret;
282  int64_t off = avio_tell(s->pb);
283 
284  for (i = 0; i < MAX_FRAME_HEADER; i++) {
285  header[i] = avio_r8(s->pb);
286  if (header[i] == '\n') {
287  header[i + 1] = 0;
288  break;
289  }
290  }
291  if (s->pb->error)
292  return s->pb->error;
293  else if (s->pb->eof_reached)
294  return AVERROR_EOF;
295  else if (i == MAX_FRAME_HEADER)
296  return AVERROR_INVALIDDATA;
297 
298  if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
299  return AVERROR_INVALIDDATA;
300 
301  ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
302  if (ret < 0)
303  return ret;
304  else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
305  av_packet_unref(pkt);
306  return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
307  }
308  pkt->stream_index = 0;
309  pkt->pts = (off - s->internal->data_offset) / s->packet_size;
310  pkt->duration = 1;
311  return 0;
312 }
313 
314 static int yuv4_read_seek(AVFormatContext *s, int stream_index,
315  int64_t pts, int flags)
316 {
317  if (avio_seek(s->pb, pts * s->packet_size + s->internal->data_offset, SEEK_SET) < 0)
318  return -1;
319  return 0;
320 }
321 
322 static int yuv4_probe(AVProbeData *pd)
323 {
324  /* check file header */
325  if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
326  return AVPROBE_SCORE_MAX;
327  else
328  return 0;
329 }
330 
332  .name = "yuv4mpegpipe",
333  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
334  .read_probe = yuv4_probe,
335  .read_header = yuv4_read_header,
336  .read_packet = yuv4_read_packet,
337  .read_seek = yuv4_read_seek,
338  .extensions = "y4m",
339 };
enum AVChromaLocation chroma_location
Definition: avcodec.h:3974
unsigned int packet_size
Definition: avformat.h:1466
#define NULL
Definition: coverity.c:32
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3965
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static enum AVPixelFormat pix_fmt
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:520
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:378
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
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:4811
int64_t data_offset
offset of the first packet
Definition: internal.h:82
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1793
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:349
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
static AVPacket pkt
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:350
Format I/O context.
Definition: avformat.h:1342
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:351
int width
Video only.
Definition: avcodec.h:3950
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
#define height
static int flags
Definition: log.c:55
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:381
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
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
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 const uint8_t header[24]
Definition: sdr2.c:67
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:373
#define av_log(a,...)
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
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:431
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegdec.c:277
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
uint16_t width
Definition: gdv.c:47
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:366
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:352
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:522
static int yuv4_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: yuv4mpegdec.c:314
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:367
Stream structure.
Definition: avformat.h:873
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:379
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:376
static int yuv4_read_header(AVFormatContext *s)
Definition: yuv4mpegdec.c:32
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFieldOrder
Definition: avcodec.h:1496
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:365
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:377
static int64_t pts
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
int eof_reached
true if eof reached
Definition: avio.h:239
AVInputFormat ff_yuv4mpegpipe_demuxer
Definition: yuv4mpegdec.c:331
static int yuv4_probe(AVProbeData *pd)
Definition: yuv4mpegdec.c:322
#define Y4M_FRAME_MAGIC_LEN
Definition: yuv4mpeg.h:26
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:518
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:521
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int stream_index
Definition: avcodec.h:1432
#define MAX_YUV4_HEADER
Definition: yuv4mpegdec.c:29
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:380
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423
#define MAX_FRAME_HEADER
Definition: yuv4mpegdec.c:30