FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
yuv4mpegenc.c
Go to the documentation of this file.
1 /*
2  * YUV4MPEG muxer
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 "yuv4mpeg.h"
26 
27 #define Y4M_LINE_MAX 256
28 
30 {
31  AVStream *st;
32  int width, height;
33  int raten, rated, aspectn, aspectd, n;
34  char inter;
35  const char *colorspace = "";
36  const char *colorrange = "";
37  int field_order;
38 
39  st = s->streams[0];
40  width = st->codecpar->width;
41  height = st->codecpar->height;
42  field_order = st->codecpar->field_order;
43 
44  // TODO: should be avg_frame_rate
45  av_reduce(&raten, &rated, st->time_base.den,
46  st->time_base.num, (1UL << 31) - 1);
47 
48  aspectn = st->sample_aspect_ratio.num;
49  aspectd = st->sample_aspect_ratio.den;
50 
51  if (aspectn == 0 && aspectd == 1)
52  aspectd = 0; // 0:0 means unknown
53 
54 #if FF_API_LAVF_AVCTX
56  if (field_order != st->codec->field_order && st->codec->field_order != AV_FIELD_UNKNOWN)
57  field_order = st->codec->field_order;
59 #endif
60 
61  switch(st->codecpar->color_range) {
62  case AVCOL_RANGE_MPEG:
63  colorrange = " XCOLORRANGE=LIMITED";
64  break;
65  case AVCOL_RANGE_JPEG:
66  colorrange = " XCOLORRANGE=FULL";
67  break;
68  default:
69  break;
70  }
71 
72  switch (field_order) {
73  case AV_FIELD_TB:
74  case AV_FIELD_TT: inter = 't'; break;
75  case AV_FIELD_BT:
76  case AV_FIELD_BB: inter = 'b'; break;
77  default: inter = 'p'; break;
78  }
79 
80  switch (st->codecpar->format) {
81  case AV_PIX_FMT_GRAY8:
82  colorspace = " Cmono";
83  break;
84  case AV_PIX_FMT_GRAY9:
85  colorspace = " Cmono9";
86  break;
87  case AV_PIX_FMT_GRAY10:
88  colorspace = " Cmono10";
89  break;
90  case AV_PIX_FMT_GRAY12:
91  colorspace = " Cmono12";
92  break;
93  case AV_PIX_FMT_GRAY16:
94  colorspace = " Cmono16";
95  break;
96  case AV_PIX_FMT_YUV411P:
97  colorspace = " C411 XYSCSS=411";
98  break;
100  colorspace = " C420jpeg XYSCSS=420JPEG";
101  colorrange = " XCOLORRANGE=FULL";
102  break;
103  case AV_PIX_FMT_YUVJ422P:
104  colorspace = " C422 XYSCSS=422";
105  colorrange = " XCOLORRANGE=FULL";
106  break;
107  case AV_PIX_FMT_YUVJ444P:
108  colorspace = " C444 XYSCSS=444";
109  colorrange = " XCOLORRANGE=FULL";
110  break;
111  case AV_PIX_FMT_YUV420P:
112  switch (st->codecpar->chroma_location) {
113  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
114  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
115  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
116  }
117  break;
118  case AV_PIX_FMT_YUV422P:
119  colorspace = " C422 XYSCSS=422";
120  break;
121  case AV_PIX_FMT_YUV444P:
122  colorspace = " C444 XYSCSS=444";
123  break;
124  case AV_PIX_FMT_YUV420P9:
125  colorspace = " C420p9 XYSCSS=420P9";
126  break;
127  case AV_PIX_FMT_YUV422P9:
128  colorspace = " C422p9 XYSCSS=422P9";
129  break;
130  case AV_PIX_FMT_YUV444P9:
131  colorspace = " C444p9 XYSCSS=444P9";
132  break;
134  colorspace = " C420p10 XYSCSS=420P10";
135  break;
137  colorspace = " C422p10 XYSCSS=422P10";
138  break;
140  colorspace = " C444p10 XYSCSS=444P10";
141  break;
143  colorspace = " C420p12 XYSCSS=420P12";
144  break;
146  colorspace = " C422p12 XYSCSS=422P12";
147  break;
149  colorspace = " C444p12 XYSCSS=444P12";
150  break;
152  colorspace = " C420p14 XYSCSS=420P14";
153  break;
155  colorspace = " C422p14 XYSCSS=422P14";
156  break;
158  colorspace = " C444p14 XYSCSS=444P14";
159  break;
161  colorspace = " C420p16 XYSCSS=420P16";
162  break;
164  colorspace = " C422p16 XYSCSS=422P16";
165  break;
167  colorspace = " C444p16 XYSCSS=444P16";
168  break;
169  }
170 
171  /* construct stream header, if this is the first frame */
172  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s%s\n",
173  Y4M_MAGIC, width, height, raten, rated, inter,
174  aspectn, aspectd, colorspace, colorrange);
175 
176  return n;
177 }
178 
179 
181 {
182  AVStream *st = s->streams[pkt->stream_index];
183  AVIOContext *pb = s->pb;
184  AVFrame *frame;
185  int* first_pkt = s->priv_data;
186  int width, height, h_chroma_shift, v_chroma_shift;
187  int i;
188  char buf2[Y4M_LINE_MAX + 1];
189  uint8_t *ptr, *ptr1, *ptr2;
190 
191  frame = (AVFrame *)pkt->data;
192 
193  /* for the first packet we have to output the header as well */
194  if (*first_pkt) {
195  *first_pkt = 0;
196  if (yuv4_generate_header(s, buf2) < 0) {
197  av_log(s, AV_LOG_ERROR,
198  "Error. YUV4MPEG stream header write failed.\n");
199  return AVERROR(EIO);
200  } else {
201  avio_write(pb, buf2, strlen(buf2));
202  }
203  }
204 
205  /* construct frame header */
206 
207  avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
208 
209  width = st->codecpar->width;
210  height = st->codecpar->height;
211 
212  ptr = frame->data[0];
213 
214  switch (st->codecpar->format) {
215  case AV_PIX_FMT_GRAY8:
216  case AV_PIX_FMT_YUV411P:
217  case AV_PIX_FMT_YUV420P:
218  case AV_PIX_FMT_YUV422P:
219  case AV_PIX_FMT_YUV444P:
220  // TODO: remove YUVJ pixel formats when they are completely removed from the codebase.
221  case AV_PIX_FMT_YUVJ420P:
222  case AV_PIX_FMT_YUVJ422P:
223  case AV_PIX_FMT_YUVJ444P:
224  break;
225  case AV_PIX_FMT_GRAY9:
226  case AV_PIX_FMT_GRAY10:
227  case AV_PIX_FMT_GRAY12:
228  case AV_PIX_FMT_GRAY16:
229  case AV_PIX_FMT_YUV420P9:
230  case AV_PIX_FMT_YUV422P9:
231  case AV_PIX_FMT_YUV444P9:
244  width *= 2;
245  break;
246  default:
247  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
249  return AVERROR(EINVAL);
250  }
251 
252  for (i = 0; i < height; i++) {
253  avio_write(pb, ptr, width);
254  ptr += frame->linesize[0];
255  }
256 
260  // Adjust for smaller Cb and Cr planes
261  av_pix_fmt_get_chroma_sub_sample(st->codecpar->format, &h_chroma_shift,
262  &v_chroma_shift);
263  // Shift right, rounding up
264  width = AV_CEIL_RSHIFT(width, h_chroma_shift);
265  height = AV_CEIL_RSHIFT(height, v_chroma_shift);
266 
267  ptr1 = frame->data[1];
268  ptr2 = frame->data[2];
269  for (i = 0; i < height; i++) { /* Cb */
270  avio_write(pb, ptr1, width);
271  ptr1 += frame->linesize[1];
272  }
273  for (i = 0; i < height; i++) { /* Cr */
274  avio_write(pb, ptr2, width);
275  ptr2 += frame->linesize[2];
276  }
277  }
278 
279  return 0;
280 }
281 
283 {
284  int *first_pkt = s->priv_data;
285 
286  if (s->nb_streams != 1)
287  return AVERROR(EIO);
288 
290  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
291  return AVERROR_INVALIDDATA;
292  }
293 
294  switch (s->streams[0]->codecpar->format) {
295  case AV_PIX_FMT_YUV411P:
296  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
297  "stream, some mjpegtools might not work.\n");
298  break;
299  case AV_PIX_FMT_GRAY8:
300  case AV_PIX_FMT_YUV420P:
301  case AV_PIX_FMT_YUV422P:
302  case AV_PIX_FMT_YUV444P:
303  // TODO: remove YUVJ pixel formats when they are completely removed from the codebase.
304  case AV_PIX_FMT_YUVJ420P:
305  case AV_PIX_FMT_YUVJ422P:
306  case AV_PIX_FMT_YUVJ444P:
307  break;
308  case AV_PIX_FMT_GRAY9:
309  case AV_PIX_FMT_GRAY10:
310  case AV_PIX_FMT_GRAY12:
311  case AV_PIX_FMT_GRAY16:
312  case AV_PIX_FMT_YUV420P9:
313  case AV_PIX_FMT_YUV422P9:
314  case AV_PIX_FMT_YUV444P9:
328  av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
329  "Use '-strict -1' to encode to this pixel format.\n",
331  return AVERROR(EINVAL);
332  }
333  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
334  "Mjpegtools will not work.\n");
335  break;
336  default:
337  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
338  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
339  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
340  "yuv444p10, yuv422p10, yuv420p10, "
341  "yuv444p12, yuv422p12, yuv420p12, "
342  "yuv444p14, yuv422p14, yuv420p14, "
343  "yuv444p16, yuv422p16, yuv420p16, "
344  "gray9, gray10, gray12 "
345  "and gray16 pixel formats. "
346  "Use -pix_fmt to select one.\n");
347  return AVERROR(EIO);
348  }
349 
350  *first_pkt = 1;
351  return 0;
352 }
353 
355  .name = "yuv4mpegpipe",
356  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
357  .extensions = "y4m",
358  .priv_data_size = sizeof(int),
359  .audio_codec = AV_CODEC_ID_NONE,
360  .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
363 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:689
enum AVChromaLocation chroma_location
Definition: avcodec.h:3990
AVOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:354
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3981
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:533
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:389
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:180
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
int num
Numerator.
Definition: rational.h:59
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:359
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:383
static AVPacket pkt
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1659
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:360
Format I/O context.
Definition: avformat.h:1351
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:361
uint8_t
int width
Video only.
Definition: avcodec.h:3966
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1445
#define Y4M_LINE_MAX
Definition: yuv4mpegenc.c:27
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:392
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
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:384
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
#define av_log(a,...)
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2474
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:377
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:363
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define width
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:535
const char * name
Definition: avformat.h:507
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:684
enum AVColorRange color_range
Video only.
Definition: avcodec.h:3986
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:700
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:378
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
Stream structure.
Definition: avformat.h:874
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:390
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:387
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
void * buf
Definition: avisynth_c.h:690
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2595
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:376
#define snprintf
Definition: snprintf.h:34
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:388
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:386
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:511
Main libavformat public API header.
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
if(ret< 0)
Definition: vf_mcdeint.c:279
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
int den
Denominator.
Definition: rational.h:60
static int yuv4_generate_header(AVFormatContext *s, char *buf)
Definition: yuv4mpegenc.c:29
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
void * priv_data
Format private data.
Definition: avformat.h:1379
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2362
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:282
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:391
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58