FFmpeg
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 
28 {
29  AVStream *st;
30  AVIOContext *pb = s->pb;
31  int width, height;
32  int raten, rated, aspectn, aspectd, ret;
33  char inter;
34  const char *colorspace = "";
35  const char *colorrange = "";
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  switch(st->codecpar->color_range) {
54  case AVCOL_RANGE_MPEG:
55  colorrange = " XCOLORRANGE=LIMITED";
56  break;
57  case AVCOL_RANGE_JPEG:
58  colorrange = " XCOLORRANGE=FULL";
59  break;
60  default:
61  break;
62  }
63 
64  switch (field_order) {
65  case AV_FIELD_TB:
66  case AV_FIELD_TT: inter = 't'; break;
67  case AV_FIELD_BT:
68  case AV_FIELD_BB: inter = 'b'; break;
69  default: inter = 'p'; break;
70  }
71 
72  switch (st->codecpar->format) {
73  case AV_PIX_FMT_GRAY8:
74  colorspace = " Cmono";
75  break;
76  case AV_PIX_FMT_GRAY9:
77  colorspace = " Cmono9";
78  break;
79  case AV_PIX_FMT_GRAY10:
80  colorspace = " Cmono10";
81  break;
82  case AV_PIX_FMT_GRAY12:
83  colorspace = " Cmono12";
84  break;
85  case AV_PIX_FMT_GRAY16:
86  colorspace = " Cmono16";
87  break;
88  case AV_PIX_FMT_YUV411P:
89  colorspace = " C411 XYSCSS=411";
90  break;
92  colorspace = " C420jpeg XYSCSS=420JPEG";
93  colorrange = " XCOLORRANGE=FULL";
94  break;
96  colorspace = " C422 XYSCSS=422";
97  colorrange = " XCOLORRANGE=FULL";
98  break;
100  colorspace = " C444 XYSCSS=444";
101  colorrange = " XCOLORRANGE=FULL";
102  break;
103  case AV_PIX_FMT_YUV420P:
104  switch (st->codecpar->chroma_location) {
105  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
106  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
107  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
108  }
109  break;
110  case AV_PIX_FMT_YUV422P:
111  colorspace = " C422 XYSCSS=422";
112  break;
113  case AV_PIX_FMT_YUV444P:
114  colorspace = " C444 XYSCSS=444";
115  break;
116  case AV_PIX_FMT_YUVA444P:
117  colorspace = " C444alpha XYSCSS=444";
118  break;
119  case AV_PIX_FMT_YUV420P9:
120  colorspace = " C420p9 XYSCSS=420P9";
121  break;
122  case AV_PIX_FMT_YUV422P9:
123  colorspace = " C422p9 XYSCSS=422P9";
124  break;
125  case AV_PIX_FMT_YUV444P9:
126  colorspace = " C444p9 XYSCSS=444P9";
127  break;
129  colorspace = " C420p10 XYSCSS=420P10";
130  break;
132  colorspace = " C422p10 XYSCSS=422P10";
133  break;
135  colorspace = " C444p10 XYSCSS=444P10";
136  break;
138  colorspace = " C420p12 XYSCSS=420P12";
139  break;
141  colorspace = " C422p12 XYSCSS=422P12";
142  break;
144  colorspace = " C444p12 XYSCSS=444P12";
145  break;
147  colorspace = " C420p14 XYSCSS=420P14";
148  break;
150  colorspace = " C422p14 XYSCSS=422P14";
151  break;
153  colorspace = " C444p14 XYSCSS=444P14";
154  break;
156  colorspace = " C420p16 XYSCSS=420P16";
157  break;
159  colorspace = " C422p16 XYSCSS=422P16";
160  break;
162  colorspace = " C444p16 XYSCSS=444P16";
163  break;
164  }
165 
166  ret = avio_printf(pb, Y4M_MAGIC " W%d H%d F%d:%d I%c A%d:%d%s%s\n",
167  width, height, raten, rated, inter,
168  aspectn, aspectd, colorspace, colorrange);
169  if (ret < 0) {
171  "Error. YUV4MPEG stream header write failed.\n");
172  return ret;
173  }
174 
175  return 0;
176 }
177 
178 
180 {
181  AVStream *st = s->streams[pkt->stream_index];
182  AVIOContext *pb = s->pb;
183  const AVFrame *frame = (const AVFrame *)pkt->data;
184  int width, height;
185  const AVPixFmtDescriptor *desc;
186 
187  /* construct frame header */
188 
189  avio_printf(s->pb, Y4M_FRAME_MAGIC "\n");
190 
191  width = st->codecpar->width;
192  height = st->codecpar->height;
194 
195  /* The following code presumes all planes to be non-interleaved. */
196  for (int k = 0; k < desc->nb_components; k++) {
197  int plane_height = height, plane_width = width * desc->comp[k].step;
198  const uint8_t *ptr = frame->data[k];
199 
200  if (desc->nb_components >= 3 && (k == 1 || k == 2)) { /* chroma? */
201  plane_width = AV_CEIL_RSHIFT(plane_width, desc->log2_chroma_w);
202  plane_height = AV_CEIL_RSHIFT(plane_height, desc->log2_chroma_h);
203  }
204 
205  for (int i = 0; i < plane_height; i++) {
206  avio_write(pb, ptr, plane_width);
207  ptr += frame->linesize[k];
208  }
209  }
210 
211  return 0;
212 }
213 
215 {
216  if (s->nb_streams != 1)
217  return AVERROR(EIO);
218 
219  if (s->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
220  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
221  return AVERROR_INVALIDDATA;
222  }
223 
224  switch (s->streams[0]->codecpar->format) {
225  case AV_PIX_FMT_YUV411P:
226  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
227  "stream, some mjpegtools might not work.\n");
228  break;
229  case AV_PIX_FMT_GRAY8:
230  case AV_PIX_FMT_YUV420P:
231  case AV_PIX_FMT_YUV422P:
232  case AV_PIX_FMT_YUV444P:
233  // TODO: remove YUVJ pixel formats when they are completely removed from the codebase.
234  case AV_PIX_FMT_YUVJ420P:
235  case AV_PIX_FMT_YUVJ422P:
236  case AV_PIX_FMT_YUVJ444P:
237  break;
238  case AV_PIX_FMT_GRAY9:
239  case AV_PIX_FMT_GRAY10:
240  case AV_PIX_FMT_GRAY12:
241  case AV_PIX_FMT_GRAY16:
242  case AV_PIX_FMT_YUV420P9:
243  case AV_PIX_FMT_YUV422P9:
244  case AV_PIX_FMT_YUV444P9:
257  case AV_PIX_FMT_YUVA444P:
258  if (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
259  av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
260  "Use '-strict -1' to encode to this pixel format.\n",
261  av_get_pix_fmt_name(s->streams[0]->codecpar->format));
262  return AVERROR(EINVAL);
263  }
264  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
265  "Mjpegtools will not work.\n");
266  break;
267  default:
268  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
269  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
270  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
271  "yuv444p10, yuv422p10, yuv420p10, "
272  "yuv444p12, yuv422p12, yuv420p12, "
273  "yuv444p14, yuv422p14, yuv420p14, "
274  "yuv444p16, yuv422p16, yuv420p16, "
275  "yuva444p, "
276  "gray9, gray10, gray12 "
277  "and gray16 pixel formats. "
278  "Use -pix_fmt to select one.\n");
279  return AVERROR(EIO);
280  }
281 
282  return 0;
283 }
284 
286  .name = "yuv4mpegpipe",
287  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
288  .extensions = "y4m",
289  .audio_codec = AV_CODEC_ID_NONE,
290  .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
291  .init = yuv4_init,
292  .write_header = yuv4_write_header,
293  .write_packet = yuv4_write_packet,
294 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
yuv4_init
static int yuv4_init(AVFormatContext *s)
Definition: yuv4mpegenc.c:214
AVOutputFormat::name
const char * name
Definition: avformat.h:504
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:597
AVPacket::data
uint8_t * data
Definition: packet.h:373
Y4M_MAGIC
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:404
AV_PIX_FMT_GRAY9
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:384
AV_FIELD_TT
@ AV_FIELD_TT
Definition: codec_par.h:39
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:402
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:388
AV_FIELD_TB
@ AV_FIELD_TB
Definition: codec_par.h:41
av_reduce
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
Y4M_FRAME_MAGIC
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:407
yuv4_write_packet
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:179
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:416
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:417
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:401
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:415
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AV_CODEC_ID_WRAPPED_AVFRAME
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: codec_id.h:572
AV_FIELD_BT
@ AV_FIELD_BT
Definition: codec_par.h:42
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:385
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:965
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:618
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:620
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:405
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:409
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:411
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:1004
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
height
#define height
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1283
yuv4_write_header
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:27
AV_FIELD_BB
@ AV_FIELD_BB
Definition: codec_par.h:40
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecParameters::height
int height
Definition: codec_par.h:127
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
yuv4mpeg.h
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:580
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:403
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avformat.h
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:408
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:150
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:413
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVPacket::stream_index
int stream_index
Definition: packet.h:375
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
desc
const char * desc
Definition: libsvtav1.c:79
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:414
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:386
ff_yuv4mpegpipe_muxer
const AVOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:285
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:412
av_get_pix_fmt_name
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:2580