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 "mux.h"
26 #include "yuv4mpeg.h"
27 
29 {
30  AVStream *st;
31  AVIOContext *pb = s->pb;
32  int width, height;
33  int raten, rated, aspectn, aspectd, ret;
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  switch(st->codecpar->color_range) {
55  case AVCOL_RANGE_MPEG:
56  colorrange = " XCOLORRANGE=LIMITED";
57  break;
58  case AVCOL_RANGE_JPEG:
59  colorrange = " XCOLORRANGE=FULL";
60  break;
61  default:
62  break;
63  }
64 
65  switch (field_order) {
66  case AV_FIELD_TB:
67  case AV_FIELD_TT: inter = 't'; break;
68  case AV_FIELD_BT:
69  case AV_FIELD_BB: inter = 'b'; break;
70  default: inter = 'p'; break;
71  }
72 
73  switch (st->codecpar->format) {
74  case AV_PIX_FMT_GRAY8:
75  colorspace = " Cmono";
76  break;
77  case AV_PIX_FMT_GRAY9:
78  colorspace = " Cmono9";
79  break;
80  case AV_PIX_FMT_GRAY10:
81  colorspace = " Cmono10";
82  break;
83  case AV_PIX_FMT_GRAY12:
84  colorspace = " Cmono12";
85  break;
86  case AV_PIX_FMT_GRAY16:
87  colorspace = " Cmono16";
88  break;
89  case AV_PIX_FMT_YUV411P:
90  colorspace = " C411 XYSCSS=411";
91  break;
93  colorspace = " C420jpeg XYSCSS=420JPEG";
94  colorrange = " XCOLORRANGE=FULL";
95  break;
97  colorspace = " C422 XYSCSS=422";
98  colorrange = " XCOLORRANGE=FULL";
99  break;
100  case AV_PIX_FMT_YUVJ444P:
101  colorspace = " C444 XYSCSS=444";
102  colorrange = " XCOLORRANGE=FULL";
103  break;
104  case AV_PIX_FMT_YUV420P:
105  switch (st->codecpar->chroma_location) {
106  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
107  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
108  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
109  }
110  break;
111  case AV_PIX_FMT_YUV422P:
112  colorspace = " C422 XYSCSS=422";
113  break;
114  case AV_PIX_FMT_YUV444P:
115  colorspace = " C444 XYSCSS=444";
116  break;
117  case AV_PIX_FMT_YUVA444P:
118  colorspace = " C444alpha XYSCSS=444";
119  break;
120  case AV_PIX_FMT_YUV420P9:
121  colorspace = " C420p9 XYSCSS=420P9";
122  break;
123  case AV_PIX_FMT_YUV422P9:
124  colorspace = " C422p9 XYSCSS=422P9";
125  break;
126  case AV_PIX_FMT_YUV444P9:
127  colorspace = " C444p9 XYSCSS=444P9";
128  break;
130  colorspace = " C420p10 XYSCSS=420P10";
131  break;
133  colorspace = " C422p10 XYSCSS=422P10";
134  break;
136  colorspace = " C444p10 XYSCSS=444P10";
137  break;
139  colorspace = " C420p12 XYSCSS=420P12";
140  break;
142  colorspace = " C422p12 XYSCSS=422P12";
143  break;
145  colorspace = " C444p12 XYSCSS=444P12";
146  break;
148  colorspace = " C420p14 XYSCSS=420P14";
149  break;
151  colorspace = " C422p14 XYSCSS=422P14";
152  break;
154  colorspace = " C444p14 XYSCSS=444P14";
155  break;
157  colorspace = " C420p16 XYSCSS=420P16";
158  break;
160  colorspace = " C422p16 XYSCSS=422P16";
161  break;
163  colorspace = " C444p16 XYSCSS=444P16";
164  break;
165  }
166 
167  ret = avio_printf(pb, Y4M_MAGIC " W%d H%d F%d:%d I%c A%d:%d%s%s\n",
168  width, height, raten, rated, inter,
169  aspectn, aspectd, colorspace, colorrange);
170  if (ret < 0) {
172  "Error. YUV4MPEG stream header write failed.\n");
173  return ret;
174  }
175 
176  return 0;
177 }
178 
179 
181 {
182  AVStream *st = s->streams[pkt->stream_index];
183  AVIOContext *pb = s->pb;
184  const AVFrame *frame = (const AVFrame *)pkt->data;
185  int width, height;
186  const AVPixFmtDescriptor *desc;
187 
188  /* construct frame header */
189 
190  avio_printf(s->pb, Y4M_FRAME_MAGIC "\n");
191 
192  width = st->codecpar->width;
193  height = st->codecpar->height;
195 
196  /* The following code presumes all planes to be non-interleaved. */
197  for (int k = 0; k < desc->nb_components; k++) {
198  int plane_height = height, plane_width = width * desc->comp[k].step;
199  const uint8_t *ptr = frame->data[k];
200 
201  if (desc->nb_components >= 3 && (k == 1 || k == 2)) { /* chroma? */
202  plane_width = AV_CEIL_RSHIFT(plane_width, desc->log2_chroma_w);
203  plane_height = AV_CEIL_RSHIFT(plane_height, desc->log2_chroma_h);
204  }
205 
206  for (int i = 0; i < plane_height; i++) {
207  avio_write(pb, ptr, plane_width);
208  ptr += frame->linesize[k];
209  }
210  }
211 
212  return 0;
213 }
214 
216 {
217  if (s->nb_streams != 1)
218  return AVERROR(EIO);
219 
220  if (s->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
221  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
222  return AVERROR_INVALIDDATA;
223  }
224 
225  switch (s->streams[0]->codecpar->format) {
226  case AV_PIX_FMT_YUV411P:
227  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
228  "stream, some mjpegtools might not work.\n");
229  break;
230  case AV_PIX_FMT_GRAY8:
231  case AV_PIX_FMT_YUV420P:
232  case AV_PIX_FMT_YUV422P:
233  case AV_PIX_FMT_YUV444P:
234  // TODO: remove YUVJ pixel formats when they are completely removed from the codebase.
235  case AV_PIX_FMT_YUVJ420P:
236  case AV_PIX_FMT_YUVJ422P:
237  case AV_PIX_FMT_YUVJ444P:
238  break;
239  case AV_PIX_FMT_GRAY9:
240  case AV_PIX_FMT_GRAY10:
241  case AV_PIX_FMT_GRAY12:
242  case AV_PIX_FMT_GRAY16:
243  case AV_PIX_FMT_YUV420P9:
244  case AV_PIX_FMT_YUV422P9:
245  case AV_PIX_FMT_YUV444P9:
258  case AV_PIX_FMT_YUVA444P:
259  if (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
260  av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
261  "Use '-strict -1' to encode to this pixel format.\n",
262  av_get_pix_fmt_name(s->streams[0]->codecpar->format));
263  return AVERROR(EINVAL);
264  }
265  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
266  "Mjpegtools will not work.\n");
267  break;
268  default:
269  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
270  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
271  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
272  "yuv444p10, yuv422p10, yuv420p10, "
273  "yuv444p12, yuv422p12, yuv420p12, "
274  "yuv444p14, yuv422p14, yuv420p14, "
275  "yuv444p16, yuv422p16, yuv420p16, "
276  "yuva444p, "
277  "gray9, gray10, gray12 "
278  "and gray16 pixel formats. "
279  "Use -pix_fmt to select one.\n");
280  return AVERROR(EIO);
281  }
282 
283  return 0;
284 }
285 
287  .p.name = "yuv4mpegpipe",
288  .p.long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
289  .p.extensions = "y4m",
290  .p.audio_codec = AV_CODEC_ID_NONE,
291  .p.video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
292  .init = yuv4_init,
293  .write_header = yuv4_write_header,
294  .write_packet = yuv4_write_packet,
295 };
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:215
AVOutputFormat::name
const char * name
Definition: avformat.h:508
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:2888
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:661
AVPacket::data
uint8_t * data
Definition: packet.h:374
Y4M_MAGIC
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:459
AV_PIX_FMT_GRAY9
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:439
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
ff_yuv4mpegpipe_muxer
const FFOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:286
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:457
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:443
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:462
yuv4_write_packet
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:180
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:471
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:256
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:472
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:50
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:128
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:456
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:470
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:594
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:440
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
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:877
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:682
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:684
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
FFOutputFormat
Definition: mux.h:30
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:460
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
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:115
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:464
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:466
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:916
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: defs.h:60
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
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: codec_par.h:41
yuv4_write_header
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:28
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecParameters::height
int height
Definition: codec_par.h:129
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:148
yuv4mpeg.h
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:644
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:458
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:143
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
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:463
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:152
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:468
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:376
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:83
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:86
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:351
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: codec_par.h:43
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_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: codec_par.h:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:469
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:441
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: codec_par.h:44
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:467
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:2808
mux.h