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  int field_order;
37 
38  st = s->streams[0];
39  width = st->codecpar->width;
40  height = st->codecpar->height;
41  field_order = st->codecpar->field_order;
42 
43  // TODO: should be avg_frame_rate
44  av_reduce(&raten, &rated, st->time_base.den,
45  st->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 #if FF_API_LAVF_AVCTX
55  if (field_order != st->codec->field_order && st->codec->field_order != AV_FIELD_UNKNOWN)
56  field_order = st->codec->field_order;
58 #endif
59 
60  switch (field_order) {
61  case AV_FIELD_TB:
62  case AV_FIELD_TT: inter = 't'; break;
63  case AV_FIELD_BT:
64  case AV_FIELD_BB: inter = 'b'; break;
65  default: inter = 'p'; break;
66  }
67 
68  switch (st->codecpar->format) {
69  case AV_PIX_FMT_GRAY8:
70  colorspace = " Cmono";
71  break;
72  case AV_PIX_FMT_GRAY9:
73  colorspace = " Cmono9";
74  break;
75  case AV_PIX_FMT_GRAY10:
76  colorspace = " Cmono10";
77  break;
78  case AV_PIX_FMT_GRAY12:
79  colorspace = " Cmono12";
80  break;
81  case AV_PIX_FMT_GRAY16:
82  colorspace = " Cmono16";
83  break;
84  case AV_PIX_FMT_YUV411P:
85  colorspace = " C411 XYSCSS=411";
86  break;
87  case AV_PIX_FMT_YUV420P:
88  switch (st->codecpar->chroma_location) {
89  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
90  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
91  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
92  }
93  break;
94  case AV_PIX_FMT_YUV422P:
95  colorspace = " C422 XYSCSS=422";
96  break;
97  case AV_PIX_FMT_YUV444P:
98  colorspace = " C444 XYSCSS=444";
99  break;
100  case AV_PIX_FMT_YUV420P9:
101  colorspace = " C420p9 XYSCSS=420P9";
102  break;
103  case AV_PIX_FMT_YUV422P9:
104  colorspace = " C422p9 XYSCSS=422P9";
105  break;
106  case AV_PIX_FMT_YUV444P9:
107  colorspace = " C444p9 XYSCSS=444P9";
108  break;
110  colorspace = " C420p10 XYSCSS=420P10";
111  break;
113  colorspace = " C422p10 XYSCSS=422P10";
114  break;
116  colorspace = " C444p10 XYSCSS=444P10";
117  break;
119  colorspace = " C420p12 XYSCSS=420P12";
120  break;
122  colorspace = " C422p12 XYSCSS=422P12";
123  break;
125  colorspace = " C444p12 XYSCSS=444P12";
126  break;
128  colorspace = " C420p14 XYSCSS=420P14";
129  break;
131  colorspace = " C422p14 XYSCSS=422P14";
132  break;
134  colorspace = " C444p14 XYSCSS=444P14";
135  break;
137  colorspace = " C420p16 XYSCSS=420P16";
138  break;
140  colorspace = " C422p16 XYSCSS=422P16";
141  break;
143  colorspace = " C444p16 XYSCSS=444P16";
144  break;
145  }
146 
147  /* construct stream header, if this is the first frame */
148  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
149  Y4M_MAGIC, width, height, raten, rated, inter,
150  aspectn, aspectd, colorspace);
151 
152  return n;
153 }
154 
156 {
157  AVStream *st = s->streams[pkt->stream_index];
158  AVIOContext *pb = s->pb;
159  AVFrame *frame;
160  int* first_pkt = s->priv_data;
161  int width, height, h_chroma_shift, v_chroma_shift;
162  int i;
163  char buf2[Y4M_LINE_MAX + 1];
164  uint8_t *ptr, *ptr1, *ptr2;
165 
166  frame = (AVFrame *)pkt->data;
167 
168  /* for the first packet we have to output the header as well */
169  if (*first_pkt) {
170  *first_pkt = 0;
171  if (yuv4_generate_header(s, buf2) < 0) {
172  av_log(s, AV_LOG_ERROR,
173  "Error. YUV4MPEG stream header write failed.\n");
174  return AVERROR(EIO);
175  } else {
176  avio_write(pb, buf2, strlen(buf2));
177  }
178  }
179 
180  /* construct frame header */
181 
182  avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
183 
184  width = st->codecpar->width;
185  height = st->codecpar->height;
186 
187  ptr = frame->data[0];
188 
189  switch (st->codecpar->format) {
190  case AV_PIX_FMT_GRAY8:
191  case AV_PIX_FMT_YUV411P:
192  case AV_PIX_FMT_YUV420P:
193  case AV_PIX_FMT_YUV422P:
194  case AV_PIX_FMT_YUV444P:
195  break;
196  case AV_PIX_FMT_GRAY9:
197  case AV_PIX_FMT_GRAY10:
198  case AV_PIX_FMT_GRAY12:
199  case AV_PIX_FMT_GRAY16:
200  case AV_PIX_FMT_YUV420P9:
201  case AV_PIX_FMT_YUV422P9:
202  case AV_PIX_FMT_YUV444P9:
215  width *= 2;
216  break;
217  default:
218  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
220  return AVERROR(EINVAL);
221  }
222 
223  for (i = 0; i < height; i++) {
224  avio_write(pb, ptr, width);
225  ptr += frame->linesize[0];
226  }
227 
231  // Adjust for smaller Cb and Cr planes
232  av_pix_fmt_get_chroma_sub_sample(st->codecpar->format, &h_chroma_shift,
233  &v_chroma_shift);
234  // Shift right, rounding up
235  width = AV_CEIL_RSHIFT(width, h_chroma_shift);
236  height = AV_CEIL_RSHIFT(height, v_chroma_shift);
237 
238  ptr1 = frame->data[1];
239  ptr2 = frame->data[2];
240  for (i = 0; i < height; i++) { /* Cb */
241  avio_write(pb, ptr1, width);
242  ptr1 += frame->linesize[1];
243  }
244  for (i = 0; i < height; i++) { /* Cr */
245  avio_write(pb, ptr2, width);
246  ptr2 += frame->linesize[2];
247  }
248  }
249 
250  return 0;
251 }
252 
254 {
255  int *first_pkt = s->priv_data;
256 
257  if (s->nb_streams != 1)
258  return AVERROR(EIO);
259 
261  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
262  return AVERROR_INVALIDDATA;
263  }
264 
265  switch (s->streams[0]->codecpar->format) {
266  case AV_PIX_FMT_YUV411P:
267  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
268  "stream, some mjpegtools might not work.\n");
269  break;
270  case AV_PIX_FMT_GRAY8:
271  case AV_PIX_FMT_YUV420P:
272  case AV_PIX_FMT_YUV422P:
273  case AV_PIX_FMT_YUV444P:
274  break;
275  case AV_PIX_FMT_GRAY9:
276  case AV_PIX_FMT_GRAY10:
277  case AV_PIX_FMT_GRAY12:
278  case AV_PIX_FMT_GRAY16:
279  case AV_PIX_FMT_YUV420P9:
280  case AV_PIX_FMT_YUV422P9:
281  case AV_PIX_FMT_YUV444P9:
295  av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
296  "Use '-strict -1' to encode to this pixel format.\n",
298  return AVERROR(EINVAL);
299  }
300  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
301  "Mjpegtools will not work.\n");
302  break;
303  default:
304  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
305  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
306  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
307  "yuv444p10, yuv422p10, yuv420p10, "
308  "yuv444p12, yuv422p12, yuv420p12, "
309  "yuv444p14, yuv422p14, yuv420p14, "
310  "yuv444p16, yuv422p16, yuv420p16, "
311  "gray9, gray10, gray12 "
312  "and gray16 pixel formats. "
313  "Use -pix_fmt to select one.\n");
314  return AVERROR(EIO);
315  }
316 
317  *first_pkt = 1;
318  return 0;
319 }
320 
322  .name = "yuv4mpegpipe",
323  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
324  .extensions = "y4m",
325  .priv_data_size = sizeof(int),
326  .audio_codec = AV_CODEC_ID_NONE,
327  .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
330 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:679
enum AVChromaLocation chroma_location
Definition: avcodec.h:3974
AVOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:321
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
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:520
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:378
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:155
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
#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:3884
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
int num
Numerator.
Definition: rational.h:59
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:349
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
static AVPacket pkt
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1648
#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
uint8_t
int width
Video only.
Definition: avcodec.h:3950
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1430
#define Y4M_LINE_MAX
Definition: yuv4mpegenc.c:27
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:381
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:373
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:2391
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
uint16_t width
Definition: gdv.c:47
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
#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
const char * name
Definition: avformat.h:507
int n
Definition: avisynth_c.h:684
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:691
#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
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
void * buf
Definition: avisynth_c.h:690
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2579
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:365
#define snprintf
Definition: snprintf.h:34
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:377
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
Main libavformat public API header.
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
if(ret< 0)
Definition: vf_mcdeint.c:279
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
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:1370
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
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:2279
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:253
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:380
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58