FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libutvideoenc.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Derek Buitenhuis
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation;
9  * version 2 of the License.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Known FOURCCs:
24  * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA),
25  * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
26  */
27 
28 extern "C" {
29 #include "libavutil/opt.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/imgutils.h"
32 #include "avcodec.h"
33 #include "internal.h"
34 }
35 
36 #include "libutvideo.h"
37 #include "put_bits.h"
38 
40 {
41  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
42  UtVideoExtra *info;
43  uint32_t flags, in_format;
44  int ret;
45 
46  switch (avctx->pix_fmt) {
47  case AV_PIX_FMT_YUV420P:
48  in_format = UTVF_YV12;
49  avctx->bits_per_coded_sample = 12;
50  if (avctx->colorspace == AVCOL_SPC_BT709)
51  avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
52  else
53  avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
54  break;
55  case AV_PIX_FMT_YUYV422:
56  in_format = UTVF_YUYV;
57  avctx->bits_per_coded_sample = 16;
58  if (avctx->colorspace == AVCOL_SPC_BT709)
59  avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
60  else
61  avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
62  break;
63  case AV_PIX_FMT_BGR24:
64  in_format = UTVF_NFCC_BGR_BU;
65  avctx->bits_per_coded_sample = 24;
66  avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
67  break;
68  case AV_PIX_FMT_RGB32:
69  in_format = UTVF_NFCC_BGRA_BU;
70  avctx->bits_per_coded_sample = 32;
71  avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
72  break;
73  default:
74  return AVERROR(EINVAL);
75  }
76 
77 #if FF_API_PRIVATE_OPT
79  if (avctx->prediction_method)
80  utv->pred = avctx->prediction_method;
82 #endif
83 
84  /* Check before we alloc anything */
85  if (utv->pred != 0 && utv->pred != 2) {
86  av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
87  return AVERROR(EINVAL);
88  }
89 
90  flags = ((utv->pred + 1) << 8) | (avctx->thread_count - 1);
91 
92  avctx->priv_data = utv;
93 
94  /* Alloc extradata buffer */
95  info = (UtVideoExtra *)av_malloc(sizeof(*info));
96 
97  if (!info) {
98  av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
99  return AVERROR(ENOMEM);
100  }
101 
102  /*
103  * We use this buffer to hold the data that Ut Video returns,
104  * since we cannot decode planes separately with it.
105  */
106  ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
107  if (ret < 0) {
108  av_free(info);
109  return ret;
110  }
111  utv->buf_size = ret;
112 
113  utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
114 
115  if (utv->buffer == NULL) {
116  av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
117  av_free(info);
118  return AVERROR(ENOMEM);
119  }
120 
121  /*
122  * Create a Ut Video instance. Since the function wants
123  * an "interface name" string, pass it the name of the lib.
124  */
125  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
126 
127  /* Initialize encoder */
128  utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
129  CBGROSSWIDTH_WINDOWS);
130 
131  /* Get extradata from encoder */
132  avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
133  utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
134  avctx->width, avctx->height,
135  CBGROSSWIDTH_WINDOWS);
136  avctx->extradata = (uint8_t *)info;
137 
138  /* Set flags */
139  utv->codec->SetState(&flags, sizeof(flags));
140 
141  return 0;
142 }
143 
145  const AVFrame *pic, int *got_packet)
146 {
147  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
148  int w = avctx->width, h = avctx->height;
149  int ret, rgb_size, i;
150  bool keyframe;
151  uint8_t *y, *u, *v;
152  uint8_t *dst;
153 
154  /* Alloc buffer */
155  if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
156  return ret;
157 
158  dst = pkt->data;
159 
160  /* Move input if needed data into Ut Video friendly buffer */
161  switch (avctx->pix_fmt) {
162  case AV_PIX_FMT_YUV420P:
163  y = utv->buffer;
164  u = y + w * h;
165  v = u + w * h / 4;
166  for (i = 0; i < h; i++) {
167  memcpy(y, pic->data[0] + i * pic->linesize[0], w);
168  y += w;
169  }
170  for (i = 0; i < h / 2; i++) {
171  memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
172  memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
173  u += w >> 1;
174  v += w >> 1;
175  }
176  break;
177  case AV_PIX_FMT_YUYV422:
178  for (i = 0; i < h; i++)
179  memcpy(utv->buffer + i * (w << 1),
180  pic->data[0] + i * pic->linesize[0], w << 1);
181  break;
182  case AV_PIX_FMT_BGR24:
183  case AV_PIX_FMT_RGB32:
184  /* Ut Video takes bottom-up BGR */
185  rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
186  for (i = 0; i < h; i++)
187  memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
188  pic->data[0] + i * pic->linesize[0],
189  w * rgb_size);
190  break;
191  default:
192  return AVERROR(EINVAL);
193  }
194 
195  /* Encode frame */
196  pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
197 
198  if (!pkt->size) {
199  av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
200  return AVERROR_INVALIDDATA;
201  }
202 
203  /*
204  * Ut Video is intra-only and every frame is a keyframe,
205  * and the API always returns true. In case something
206  * durastic changes in the future, such as inter support,
207  * assert that this is true.
208  */
209  av_assert2(keyframe == true);
210 
211  pkt->flags |= AV_PKT_FLAG_KEY;
212  *got_packet = 1;
213  return 0;
214 }
215 
217 {
218  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
219 
220  av_freep(&avctx->extradata);
221  av_freep(&utv->buffer);
222 
223  utv->codec->EncodeEnd();
224  CCodec::DeleteInstance(utv->codec);
225 
226  return 0;
227 }
228 
229 #define OFFSET(x) offsetof(UtVideoContext, x)
230 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
231 static const AVOption options[] = {
232  { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, 0, 0, 2, VE, "pred" },
233  { "left", NULL, 0, AV_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, VE, "pred" },
234  { "median", NULL, 0, AV_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, VE, "pred" },
235  { NULL },
236 };
237 
238 static const AVClass utvideo_class = {
239  "libutvideo",
241  options,
243  0,
244  0,
245  NULL,
246  NULL,
248  NULL,
249  NULL,
250 };
251 
253  "libutvideo",
254  NULL_IF_CONFIG_SMALL("Ut Video"),
258  NULL, /* supported_framerates */
259  (const enum AVPixelFormat[]) {
262  },
263  NULL, /* supported_samplerates */
264  NULL, /* sample_fmts */
265  NULL, /* channel_layouts */
266  0, /* max_lowres */
267  &utvideo_class, /* priv_class */
268  NULL, /* profiles */
269  sizeof(UtVideoContext),
270  NULL, /* next */
271  NULL, /* init_thread_copy */
272  NULL, /* update_thread_context */
273  NULL, /* defaults */
274  NULL, /* init_static_data */
276  NULL, /* encode */
278  NULL, /* decode */
280  NULL, /* flush */
281 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:422
#define NULL
Definition: coverity.c:32
static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
misc image utilities
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
int size
Definition: avcodec.h:1468
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
static AVPacket pkt
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:931
AVCodec.
Definition: avcodec.h:3392
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:939
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
uint8_t * buffer
Definition: libutvideo.h:68
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
uint8_t * data
Definition: avcodec.h:1467
static const AVClass utvideo_class
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:361
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
simple assert() macros that are a bit more flexible than ISO C assert().
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
int width
picture width / height.
Definition: avcodec.h:1711
Known FOURCCs: 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA), 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
static const AVOption options[]
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2954
static const float pred[4]
Definition: siprdata.h:259
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
Libavcodec external API header.
attribute_deprecated int prediction_method
Definition: avcodec.h:1915
#define VE
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
main external API structure.
Definition: avcodec.h:1532
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:307
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
int extradata_size
Definition: avcodec.h:1648
Describe the class of an AVClass context structure.
Definition: log.h:67
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2255
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1621
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
unsigned int buf_size
Definition: libutvideo.h:67
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:943
CCodec * codec
Definition: libutvideo.h:66
void * priv_data
Definition: avcodec.h:1574
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
#define av_freep(p)
#define MKTAG(a, b, c, d)
Definition: common.h:342
#define OFFSET(x)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
AVCodec ff_libutvideo_encoder
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46
bitstream writer API