FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
yuv4mpeg.c
Go to the documentation of this file.
1 /*
2  * YUV4MPEG format
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/pixdesc.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "libavutil/pixdesc.h"
26 
27 #define Y4M_MAGIC "YUV4MPEG2"
28 #define Y4M_FRAME_MAGIC "FRAME"
29 #define Y4M_LINE_MAX 256
30 
31 #if CONFIG_YUV4MPEGPIPE_MUXER
32 static int yuv4_generate_header(AVFormatContext *s, char* buf)
33 {
34  AVStream *st;
35  int width, height;
36  int raten, rated, aspectn, aspectd, n;
37  char inter;
38  const char *colorspace = "";
39 
40  st = s->streams[0];
41  width = st->codec->width;
42  height = st->codec->height;
43 
44  av_reduce(&raten, &rated, st->codec->time_base.den,
45  st->codec->time_base.num, (1UL << 31) - 1);
46 
47  aspectn = st->sample_aspect_ratio.num;
48  aspectd = st->sample_aspect_ratio.den;
49 
50  if (aspectn == 0 && aspectd == 1)
51  aspectd = 0; // 0:0 means unknown
52 
53  inter = 'p'; /* progressive is the default */
55  inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
57  inter = 'p';
58  } else if (st->codec->field_order == AV_FIELD_TB || st->codec->field_order == AV_FIELD_TT) {
59  inter = 't';
60  } else if (st->codec->field_order == AV_FIELD_BT || st->codec->field_order == AV_FIELD_BB) {
61  inter = 'b';
62  }
63 
64  switch (st->codec->pix_fmt) {
65  case AV_PIX_FMT_GRAY8:
66  colorspace = " Cmono";
67  break;
68  case AV_PIX_FMT_GRAY16:
69  colorspace = " Cmono16";
70  break;
71  case AV_PIX_FMT_YUV411P:
72  colorspace = " C411 XYSCSS=411";
73  break;
74  case AV_PIX_FMT_YUV420P:
75  switch (st->codec->chroma_sample_location) {
76  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
77  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
78  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
79  }
80  break;
81  case AV_PIX_FMT_YUV422P:
82  colorspace = " C422 XYSCSS=422";
83  break;
84  case AV_PIX_FMT_YUV444P:
85  colorspace = " C444 XYSCSS=444";
86  break;
88  colorspace = " C420p9 XYSCSS=420P9";
89  break;
91  colorspace = " C422p9 XYSCSS=422P9";
92  break;
94  colorspace = " C444p9 XYSCSS=444P9";
95  break;
97  colorspace = " C420p10 XYSCSS=420P10";
98  break;
100  colorspace = " C422p10 XYSCSS=422P10";
101  break;
103  colorspace = " C444p10 XYSCSS=444P10";
104  break;
106  colorspace = " C420p12 XYSCSS=420P12";
107  break;
109  colorspace = " C422p12 XYSCSS=422P12";
110  break;
112  colorspace = " C444p12 XYSCSS=444P12";
113  break;
115  colorspace = " C420p14 XYSCSS=420P14";
116  break;
118  colorspace = " C422p14 XYSCSS=422P14";
119  break;
121  colorspace = " C444p14 XYSCSS=444P14";
122  break;
124  colorspace = " C420p16 XYSCSS=420P16";
125  break;
127  colorspace = " C422p16 XYSCSS=422P16";
128  break;
130  colorspace = " C444p16 XYSCSS=444P16";
131  break;
132  }
133 
134  /* construct stream header, if this is the first frame */
135  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
136  Y4M_MAGIC, width, height, raten, rated, inter,
137  aspectn, aspectd, colorspace);
138 
139  return n;
140 }
141 
142 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
143 {
144  AVStream *st = s->streams[pkt->stream_index];
145  AVIOContext *pb = s->pb;
146  AVPicture *picture, picture_tmp;
147  int* first_pkt = s->priv_data;
148  int width, height, h_chroma_shift, v_chroma_shift;
149  int i;
150  char buf2[Y4M_LINE_MAX + 1];
151  char buf1[20];
152  uint8_t *ptr, *ptr1, *ptr2;
153 
154  memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
155  picture = &picture_tmp;
156 
157  /* for the first packet we have to output the header as well */
158  if (*first_pkt) {
159  *first_pkt = 0;
160  if (yuv4_generate_header(s, buf2) < 0) {
161  av_log(s, AV_LOG_ERROR,
162  "Error. YUV4MPEG stream header write failed.\n");
163  return AVERROR(EIO);
164  } else {
165  avio_write(pb, buf2, strlen(buf2));
166  }
167  }
168 
169  /* construct frame header */
170 
171  snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
172  avio_write(pb, buf1, strlen(buf1));
173 
174  width = st->codec->width;
175  height = st->codec->height;
176 
177  ptr = picture->data[0];
178 
179  switch (st->codec->pix_fmt) {
180  case AV_PIX_FMT_GRAY8:
181  case AV_PIX_FMT_YUV411P:
182  case AV_PIX_FMT_YUV420P:
183  case AV_PIX_FMT_YUV422P:
184  case AV_PIX_FMT_YUV444P:
185  break;
186  case AV_PIX_FMT_GRAY16:
187  case AV_PIX_FMT_YUV420P9:
188  case AV_PIX_FMT_YUV422P9:
189  case AV_PIX_FMT_YUV444P9:
202  width *= 2;
203  break;
204  default:
205  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
207  return AVERROR(EINVAL);
208  }
209 
210  for (i = 0; i < height; i++) {
211  avio_write(pb, ptr, width);
212  ptr += picture->linesize[0];
213  }
214 
215  if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
216  st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
217  // Adjust for smaller Cb and Cr planes
218  av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
219  &v_chroma_shift);
220  width >>= h_chroma_shift;
221  height >>= v_chroma_shift;
222 
223  ptr1 = picture->data[1];
224  ptr2 = picture->data[2];
225  for (i = 0; i < height; i++) { /* Cb */
226  avio_write(pb, ptr1, width);
227  ptr1 += picture->linesize[1];
228  }
229  for (i = 0; i < height; i++) { /* Cr */
230  avio_write(pb, ptr2, width);
231  ptr2 += picture->linesize[2];
232  }
233  }
234 
235  avio_flush(pb);
236  return 0;
237 }
238 
239 static int yuv4_write_header(AVFormatContext *s)
240 {
241  int *first_pkt = s->priv_data;
242 
243  if (s->nb_streams != 1)
244  return AVERROR(EIO);
245 
246  if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
247  av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
248  return AVERROR_INVALIDDATA;
249  }
250 
251  switch (s->streams[0]->codec->pix_fmt) {
252  case AV_PIX_FMT_YUV411P:
253  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
254  "stream, some mjpegtools might not work.\n");
255  break;
256  case AV_PIX_FMT_GRAY8:
257  case AV_PIX_FMT_GRAY16:
258  case AV_PIX_FMT_YUV420P:
259  case AV_PIX_FMT_YUV422P:
260  case AV_PIX_FMT_YUV444P:
261  break;
262  case AV_PIX_FMT_YUV420P9:
263  case AV_PIX_FMT_YUV422P9:
264  case AV_PIX_FMT_YUV444P9:
278  av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
279  "Use '-strict -1' to encode to this pixel format.\n",
281  return AVERROR(EINVAL);
282  }
283  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
284  "Mjpegtools will not work.\n");
285  break;
286  default:
287  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
288  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
289  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
290  "yuv444p10, yuv422p10, yuv420p10, "
291  "yuv444p12, yuv422p12, yuv420p12, "
292  "yuv444p14, yuv422p14, yuv420p14, "
293  "yuv444p16, yuv422p16, yuv420p16 "
294  "and gray16 pixel formats. "
295  "Use -pix_fmt to select one.\n");
296  return AVERROR(EIO);
297  }
298 
299  *first_pkt = 1;
300  return 0;
301 }
302 
303 AVOutputFormat ff_yuv4mpegpipe_muxer = {
304  .name = "yuv4mpegpipe",
305  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
306  .extensions = "y4m",
307  .priv_data_size = sizeof(int),
308  .audio_codec = AV_CODEC_ID_NONE,
309  .video_codec = AV_CODEC_ID_RAWVIDEO,
310  .write_header = yuv4_write_header,
311  .write_packet = yuv4_write_packet,
313 };
314 #endif
315 
316 /* Header size increased to allow room for optional flags */
317 #define MAX_YUV4_HEADER 80
318 #define MAX_FRAME_HEADER 80
319 
321 {
322  char header[MAX_YUV4_HEADER + 10]; // Include headroom for
323  // the longest option
324  char *tokstart, *tokend, *header_end, interlaced = '?';
325  int i;
326  AVIOContext *pb = s->pb;
327  int width = -1, height = -1, raten = 0,
328  rated = 0, aspectn = 0, aspectd = 0;
330  enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
331  AVStream *st;
332 
333  for (i = 0; i < MAX_YUV4_HEADER; i++) {
334  header[i] = avio_r8(pb);
335  if (header[i] == '\n') {
336  header[i + 1] = 0x20; // Add a space after last option.
337  // Makes parsing "444" vs "444alpha" easier.
338  header[i + 2] = 0;
339  break;
340  }
341  }
342  if (i == MAX_YUV4_HEADER)
343  return -1;
344  if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
345  return -1;
346 
347  header_end = &header[i + 1]; // Include space
348  for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
349  tokstart < header_end; tokstart++) {
350  if (*tokstart == 0x20)
351  continue;
352  switch (*tokstart++) {
353  case 'W': // Width. Required.
354  width = strtol(tokstart, &tokend, 10);
355  tokstart = tokend;
356  break;
357  case 'H': // Height. Required.
358  height = strtol(tokstart, &tokend, 10);
359  tokstart = tokend;
360  break;
361  case 'C': // Color space
362  if (strncmp("420jpeg", tokstart, 7) == 0) {
363  pix_fmt = AV_PIX_FMT_YUV420P;
364  chroma_sample_location = AVCHROMA_LOC_CENTER;
365  } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
366  pix_fmt = AV_PIX_FMT_YUV420P;
367  chroma_sample_location = AVCHROMA_LOC_LEFT;
368  } else if (strncmp("420paldv", tokstart, 8) == 0) {
369  pix_fmt = AV_PIX_FMT_YUV420P;
370  chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
371  } else if (strncmp("420p16", tokstart, 6) == 0) {
372  pix_fmt = AV_PIX_FMT_YUV420P16;
373  } else if (strncmp("422p16", tokstart, 6) == 0) {
374  pix_fmt = AV_PIX_FMT_YUV422P16;
375  } else if (strncmp("444p16", tokstart, 6) == 0) {
376  pix_fmt = AV_PIX_FMT_YUV444P16;
377  } else if (strncmp("420p14", tokstart, 6) == 0) {
378  pix_fmt = AV_PIX_FMT_YUV420P14;
379  } else if (strncmp("422p14", tokstart, 6) == 0) {
380  pix_fmt = AV_PIX_FMT_YUV422P14;
381  } else if (strncmp("444p14", tokstart, 6) == 0) {
382  pix_fmt = AV_PIX_FMT_YUV444P14;
383  } else if (strncmp("420p12", tokstart, 6) == 0) {
384  pix_fmt = AV_PIX_FMT_YUV420P12;
385  } else if (strncmp("422p12", tokstart, 6) == 0) {
386  pix_fmt = AV_PIX_FMT_YUV422P12;
387  } else if (strncmp("444p12", tokstart, 6) == 0) {
388  pix_fmt = AV_PIX_FMT_YUV444P12;
389  } else if (strncmp("420p10", tokstart, 6) == 0) {
390  pix_fmt = AV_PIX_FMT_YUV420P10;
391  } else if (strncmp("422p10", tokstart, 6) == 0) {
392  pix_fmt = AV_PIX_FMT_YUV422P10;
393  } else if (strncmp("444p10", tokstart, 6) == 0) {
394  pix_fmt = AV_PIX_FMT_YUV444P10;
395  } else if (strncmp("420p9", tokstart, 5) == 0) {
396  pix_fmt = AV_PIX_FMT_YUV420P9;
397  } else if (strncmp("422p9", tokstart, 5) == 0) {
398  pix_fmt = AV_PIX_FMT_YUV422P9;
399  } else if (strncmp("444p9", tokstart, 5) == 0) {
400  pix_fmt = AV_PIX_FMT_YUV444P9;
401  } else if (strncmp("420", tokstart, 3) == 0) {
402  pix_fmt = AV_PIX_FMT_YUV420P;
403  chroma_sample_location = AVCHROMA_LOC_CENTER;
404  } else if (strncmp("411", tokstart, 3) == 0) {
405  pix_fmt = AV_PIX_FMT_YUV411P;
406  } else if (strncmp("422", tokstart, 3) == 0) {
407  pix_fmt = AV_PIX_FMT_YUV422P;
408  } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
409  av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
410  "YUV4MPEG stream.\n");
411  return -1;
412  } else if (strncmp("444", tokstart, 3) == 0) {
413  pix_fmt = AV_PIX_FMT_YUV444P;
414  } else if (strncmp("mono16", tokstart, 6) == 0) {
415  pix_fmt = AV_PIX_FMT_GRAY16;
416  } else if (strncmp("mono", tokstart, 4) == 0) {
417  pix_fmt = AV_PIX_FMT_GRAY8;
418  } else {
419  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
420  "pixel format.\n");
421  return -1;
422  }
423  while (tokstart < header_end && *tokstart != 0x20)
424  tokstart++;
425  break;
426  case 'I': // Interlace type
427  interlaced = *tokstart++;
428  break;
429  case 'F': // Frame rate
430  sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
431  while (tokstart < header_end && *tokstart != 0x20)
432  tokstart++;
433  break;
434  case 'A': // Pixel aspect
435  sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
436  while (tokstart < header_end && *tokstart != 0x20)
437  tokstart++;
438  break;
439  case 'X': // Vendor extensions
440  if (strncmp("YSCSS=", tokstart, 6) == 0) {
441  // Older nonstandard pixel format representation
442  tokstart += 6;
443  if (strncmp("420JPEG", tokstart, 7) == 0)
444  alt_pix_fmt = AV_PIX_FMT_YUV420P;
445  else if (strncmp("420MPEG2", tokstart, 8) == 0)
446  alt_pix_fmt = AV_PIX_FMT_YUV420P;
447  else if (strncmp("420PALDV", tokstart, 8) == 0)
448  alt_pix_fmt = AV_PIX_FMT_YUV420P;
449  else if (strncmp("420P9", tokstart, 5) == 0)
450  alt_pix_fmt = AV_PIX_FMT_YUV420P9;
451  else if (strncmp("422P9", tokstart, 5) == 0)
452  alt_pix_fmt = AV_PIX_FMT_YUV422P9;
453  else if (strncmp("444P9", tokstart, 5) == 0)
454  alt_pix_fmt = AV_PIX_FMT_YUV444P9;
455  else if (strncmp("420P10", tokstart, 6) == 0)
456  alt_pix_fmt = AV_PIX_FMT_YUV420P10;
457  else if (strncmp("422P10", tokstart, 6) == 0)
458  alt_pix_fmt = AV_PIX_FMT_YUV422P10;
459  else if (strncmp("444P10", tokstart, 6) == 0)
460  alt_pix_fmt = AV_PIX_FMT_YUV444P10;
461  else if (strncmp("420P12", tokstart, 6) == 0)
462  alt_pix_fmt = AV_PIX_FMT_YUV420P12;
463  else if (strncmp("422P12", tokstart, 6) == 0)
464  alt_pix_fmt = AV_PIX_FMT_YUV422P12;
465  else if (strncmp("444P12", tokstart, 6) == 0)
466  alt_pix_fmt = AV_PIX_FMT_YUV444P12;
467  else if (strncmp("420P14", tokstart, 6) == 0)
468  alt_pix_fmt = AV_PIX_FMT_YUV420P14;
469  else if (strncmp("422P14", tokstart, 6) == 0)
470  alt_pix_fmt = AV_PIX_FMT_YUV422P14;
471  else if (strncmp("444P14", tokstart, 6) == 0)
472  alt_pix_fmt = AV_PIX_FMT_YUV444P14;
473  else if (strncmp("420P16", tokstart, 6) == 0)
474  alt_pix_fmt = AV_PIX_FMT_YUV420P16;
475  else if (strncmp("422P16", tokstart, 6) == 0)
476  alt_pix_fmt = AV_PIX_FMT_YUV422P16;
477  else if (strncmp("444P16", tokstart, 6) == 0)
478  alt_pix_fmt = AV_PIX_FMT_YUV444P16;
479  else if (strncmp("411", tokstart, 3) == 0)
480  alt_pix_fmt = AV_PIX_FMT_YUV411P;
481  else if (strncmp("422", tokstart, 3) == 0)
482  alt_pix_fmt = AV_PIX_FMT_YUV422P;
483  else if (strncmp("444", tokstart, 3) == 0)
484  alt_pix_fmt = AV_PIX_FMT_YUV444P;
485  }
486  while (tokstart < header_end && *tokstart != 0x20)
487  tokstart++;
488  break;
489  }
490  }
491 
492  if (width == -1 || height == -1) {
493  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
494  return -1;
495  }
496 
497  if (pix_fmt == AV_PIX_FMT_NONE) {
498  if (alt_pix_fmt == AV_PIX_FMT_NONE)
499  pix_fmt = AV_PIX_FMT_YUV420P;
500  else
501  pix_fmt = alt_pix_fmt;
502  }
503 
504  if (raten <= 0 || rated <= 0) {
505  // Frame rate unknown
506  raten = 25;
507  rated = 1;
508  }
509 
510  if (aspectn == 0 && aspectd == 0) {
511  // Pixel aspect unknown
512  aspectd = 1;
513  }
514 
515  st = avformat_new_stream(s, NULL);
516  if (!st)
517  return AVERROR(ENOMEM);
518  st->codec->width = width;
519  st->codec->height = height;
520  av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
521  avpriv_set_pts_info(st, 64, rated, raten);
522  st->codec->pix_fmt = pix_fmt;
525  st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
526  st->codec->chroma_sample_location = chroma_sample_location;
527 
528  switch (interlaced){
529  case 'p':
531  break;
532  case 't':
534  break;
535  case 'b':
537  break;
538  case 'm':
539  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
540  "interlaced and non-interlaced frames.\n");
541  case '?':
543  break;
544  default:
545  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
546  return AVERROR(EINVAL);
547  }
548 
549  return 0;
550 }
551 
553 {
554  int i;
555  char header[MAX_FRAME_HEADER+1];
556  int packet_size, width, height, ret;
557  AVStream *st = s->streams[0];
558 
559  for (i = 0; i < MAX_FRAME_HEADER; i++) {
560  header[i] = avio_r8(s->pb);
561  if (header[i] == '\n') {
562  header[i + 1] = 0;
563  break;
564  }
565  }
566  if (s->pb->error)
567  return s->pb->error;
568  else if (s->pb->eof_reached)
569  return AVERROR_EOF;
570  else if (i == MAX_FRAME_HEADER)
571  return AVERROR_INVALIDDATA;
572 
573  if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
574  return AVERROR_INVALIDDATA;
575 
576  width = st->codec->width;
577  height = st->codec->height;
578 
579  packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
580  if (packet_size < 0)
581  return packet_size;
582 
583  ret = av_get_packet(s->pb, pkt, packet_size);
584  if (ret < 0)
585  return ret;
586  else if (ret != packet_size)
587  return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
588 
589  pkt->stream_index = 0;
590  return 0;
591 }
592 
593 static int yuv4_probe(AVProbeData *pd)
594 {
595  /* check file header */
596  if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
597  return AVPROBE_SCORE_MAX;
598  else
599  return 0;
600 }
601 
602 #if CONFIG_YUV4MPEGPIPE_DEMUXER
603 AVInputFormat ff_yuv4mpegpipe_demuxer = {
604  .name = "yuv4mpegpipe",
605  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
606  .read_probe = yuv4_probe,
607  .read_header = yuv4_read_header,
608  .read_packet = yuv4_read_packet,
609  .extensions = "y4m",
610 };
611 #endif