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/avassert.h"
30 #include "avcodec.h"
31 #include "internal.h"
32 }
33 
34 #include "libutvideo.h"
35 #include "put_bits.h"
36 
38 {
39  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
40  UtVideoExtra *info;
41  uint32_t flags, in_format;
42  int ret;
43 
44  switch (avctx->pix_fmt) {
45  case AV_PIX_FMT_YUV420P:
46  in_format = UTVF_YV12;
47  avctx->bits_per_coded_sample = 12;
48  if (avctx->colorspace == AVCOL_SPC_BT709)
49  avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
50  else
51  avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
52  break;
53  case AV_PIX_FMT_YUYV422:
54  in_format = UTVF_YUYV;
55  avctx->bits_per_coded_sample = 16;
56  if (avctx->colorspace == AVCOL_SPC_BT709)
57  avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
58  else
59  avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
60  break;
61  case AV_PIX_FMT_BGR24:
62  in_format = UTVF_NFCC_BGR_BU;
63  avctx->bits_per_coded_sample = 24;
64  avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
65  break;
66  case AV_PIX_FMT_RGB32:
67  in_format = UTVF_NFCC_BGRA_BU;
68  avctx->bits_per_coded_sample = 32;
69  avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
70  break;
71  default:
72  return AVERROR(EINVAL);
73  }
74 
75  /* Check before we alloc anything */
76  if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
77  av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
78  return AVERROR(EINVAL);
79  }
80 
81  flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
82 
83  avctx->priv_data = utv;
84 
85  /* Alloc extradata buffer */
86  info = (UtVideoExtra *)av_malloc(sizeof(*info));
87 
88  if (!info) {
89  av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
90  return AVERROR(ENOMEM);
91  }
92 
93  /*
94  * We use this buffer to hold the data that Ut Video returns,
95  * since we cannot decode planes separately with it.
96  */
97  ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
98  if (ret < 0) {
99  av_free(info);
100  return ret;
101  }
102  utv->buf_size = ret;
103 
104  utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
105 
106  if (utv->buffer == NULL) {
107  av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
108  av_free(info);
109  return AVERROR(ENOMEM);
110  }
111 
112  /*
113  * Create a Ut Video instance. Since the function wants
114  * an "interface name" string, pass it the name of the lib.
115  */
116  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
117 
118  /* Initialize encoder */
119  utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
120  CBGROSSWIDTH_WINDOWS);
121 
122  /* Get extradata from encoder */
123  avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
124  utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
125  avctx->width, avctx->height,
126  CBGROSSWIDTH_WINDOWS);
127  avctx->extradata = (uint8_t *)info;
128 
129  /* Set flags */
130  utv->codec->SetState(&flags, sizeof(flags));
131 
132  return 0;
133 }
134 
136  const AVFrame *pic, int *got_packet)
137 {
138  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
139  int w = avctx->width, h = avctx->height;
140  int ret, rgb_size, i;
141  bool keyframe;
142  uint8_t *y, *u, *v;
143  uint8_t *dst;
144 
145  /* Alloc buffer */
146  if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
147  return ret;
148 
149  dst = pkt->data;
150 
151  /* Move input if needed data into Ut Video friendly buffer */
152  switch (avctx->pix_fmt) {
153  case AV_PIX_FMT_YUV420P:
154  y = utv->buffer;
155  u = y + w * h;
156  v = u + w * h / 4;
157  for (i = 0; i < h; i++) {
158  memcpy(y, pic->data[0] + i * pic->linesize[0], w);
159  y += w;
160  }
161  for (i = 0; i < h / 2; i++) {
162  memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
163  memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
164  u += w >> 1;
165  v += w >> 1;
166  }
167  break;
168  case AV_PIX_FMT_YUYV422:
169  for (i = 0; i < h; i++)
170  memcpy(utv->buffer + i * (w << 1),
171  pic->data[0] + i * pic->linesize[0], w << 1);
172  break;
173  case AV_PIX_FMT_BGR24:
174  case AV_PIX_FMT_RGB32:
175  /* Ut Video takes bottom-up BGR */
176  rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
177  for (i = 0; i < h; i++)
178  memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
179  pic->data[0] + i * pic->linesize[0],
180  w * rgb_size);
181  break;
182  default:
183  return AVERROR(EINVAL);
184  }
185 
186  /* Encode frame */
187  pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
188 
189  if (!pkt->size) {
190  av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
191  return AVERROR_INVALIDDATA;
192  }
193 
194  /*
195  * Ut Video is intra-only and every frame is a keyframe,
196  * and the API always returns true. In case something
197  * durastic changes in the future, such as inter support,
198  * assert that this is true.
199  */
200  av_assert2(keyframe == true);
201 
202  pkt->flags |= AV_PKT_FLAG_KEY;
203  *got_packet = 1;
204  return 0;
205 }
206 
208 {
209  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
210 
211  av_freep(&avctx->extradata);
212  av_freep(&utv->buffer);
213 
214  utv->codec->EncodeEnd();
215  CCodec::DeleteInstance(utv->codec);
216 
217  return 0;
218 }
219 
221  "libutvideo",
222  NULL_IF_CONFIG_SMALL("Ut Video"),
226  NULL, /* supported_framerates */
227  (const enum AVPixelFormat[]) {
230  },
231  NULL, /* supported_samplerates */
232  NULL, /* sample_fmts */
233  NULL, /* channel_layouts */
234  0, /* max_lowres */
235  NULL, /* priv_class */
236  NULL, /* profiles */
237  sizeof(UtVideoContext),
238  NULL, /* next */
239  NULL, /* init_thread_copy */
240  NULL, /* update_thread_context */
241  NULL, /* defaults */
242  NULL, /* init_static_data */
244  NULL, /* encode */
246  NULL, /* decode */
248  NULL, /* flush */
249 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:438
#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:184
int size
Definition: avcodec.h:1589
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1885
static AVPacket pkt
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1031
AVCodec.
Definition: avcodec.h:3559
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:1039
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
uint8_t * buffer
Definition: libutvideo.h:67
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1772
uint8_t * data
Definition: avcodec.h:1588
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3050
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1620
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#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:1594
int width
picture width / height.
Definition: avcodec.h:1844
attribute_deprecated int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Definition: avpicture.c:52
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:65
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3087
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
Libavcodec external API header.
attribute_deprecated int prediction_method
Definition: avcodec.h:2048
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
main external API structure.
Definition: avcodec.h:1657
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:320
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1689
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
int extradata_size
Definition: avcodec.h:1773
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2386
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:1690
#define u(width,...)
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
unsigned int buf_size
Definition: libutvideo.h:66
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:1043
CCodec * codec
Definition: libutvideo.h:65
void * priv_data
Definition: avcodec.h:1699
#define av_free(p)
#define av_freep(p)
#define MKTAG(a, b, c, d)
Definition: common.h:342
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1565
AVCodec ff_libutvideo_encoder
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46
bitstream writer API