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  avctx->coded_frame = av_frame_alloc();
85 
86  /* Alloc extradata buffer */
87  info = (UtVideoExtra *)av_malloc(sizeof(*info));
88 
89  if (!info) {
90  av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
91  return AVERROR(ENOMEM);
92  }
93 
94  /*
95  * We use this buffer to hold the data that Ut Video returns,
96  * since we cannot decode planes separately with it.
97  */
98  ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
99  if (ret < 0) {
100  av_free(info);
101  return ret;
102  }
103  utv->buf_size = ret;
104 
105  utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
106 
107  if (utv->buffer == NULL) {
108  av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
109  av_free(info);
110  return AVERROR(ENOMEM);
111  }
112 
113  /*
114  * Create a Ut Video instance. Since the function wants
115  * an "interface name" string, pass it the name of the lib.
116  */
117  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
118 
119  /* Initialize encoder */
120  utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
121  CBGROSSWIDTH_WINDOWS);
122 
123  /* Get extradata from encoder */
124  avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
125  utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
126  avctx->width, avctx->height,
127  CBGROSSWIDTH_WINDOWS);
128  avctx->extradata = (uint8_t *)info;
129 
130  /* Set flags */
131  utv->codec->SetState(&flags, sizeof(flags));
132 
133  return 0;
134 }
135 
137  const AVFrame *pic, int *got_packet)
138 {
139  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
140  int w = avctx->width, h = avctx->height;
141  int ret, rgb_size, i;
142  bool keyframe;
143  uint8_t *y, *u, *v;
144  uint8_t *dst;
145 
146  /* Alloc buffer */
147  if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
148  return ret;
149 
150  dst = pkt->data;
151 
152  /* Move input if needed data into Ut Video friendly buffer */
153  switch (avctx->pix_fmt) {
154  case AV_PIX_FMT_YUV420P:
155  y = utv->buffer;
156  u = y + w * h;
157  v = u + w * h / 4;
158  for (i = 0; i < h; i++) {
159  memcpy(y, pic->data[0] + i * pic->linesize[0], w);
160  y += w;
161  }
162  for (i = 0; i < h / 2; i++) {
163  memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
164  memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
165  u += w >> 1;
166  v += w >> 1;
167  }
168  break;
169  case AV_PIX_FMT_YUYV422:
170  for (i = 0; i < h; i++)
171  memcpy(utv->buffer + i * (w << 1),
172  pic->data[0] + i * pic->linesize[0], w << 1);
173  break;
174  case AV_PIX_FMT_BGR24:
175  case AV_PIX_FMT_RGB32:
176  /* Ut Video takes bottom-up BGR */
177  rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
178  for (i = 0; i < h; i++)
179  memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
180  pic->data[0] + i * pic->linesize[0],
181  w * rgb_size);
182  break;
183  default:
184  return AVERROR(EINVAL);
185  }
186 
187  /* Encode frame */
188  pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
189 
190  if (!pkt->size) {
191  av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
192  return AVERROR_INVALIDDATA;
193  }
194 
195  /*
196  * Ut Video is intra-only and every frame is a keyframe,
197  * and the API always returns true. In case something
198  * durastic changes in the future, such as inter support,
199  * assert that this is true.
200  */
201  av_assert2(keyframe == true);
202  avctx->coded_frame->key_frame = 1;
204 
205  pkt->flags |= AV_PKT_FLAG_KEY;
206  *got_packet = 1;
207  return 0;
208 }
209 
211 {
212  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
213 
214  av_frame_free(&avctx->coded_frame);
215  av_freep(&avctx->extradata);
216  av_freep(&utv->buffer);
217 
218  utv->codec->EncodeEnd();
219  CCodec::DeleteInstance(utv->codec);
220 
221  return 0;
222 }
223 
225  "libutvideo",
226  NULL_IF_CONFIG_SMALL("Ut Video"),
230  NULL, /* supported_framerates */
231  (const enum AVPixelFormat[]) {
234  },
235  NULL, /* supported_samplerates */
236  NULL, /* sample_fmts */
237  NULL, /* channel_layouts */
238  0, /* max_lowres */
239  NULL, /* priv_class */
240  NULL, /* profiles */
241  sizeof(UtVideoContext),
242  NULL, /* next */
243  NULL, /* init_thread_copy */
244  NULL, /* update_thread_context */
245  NULL, /* defaults */
246  NULL, /* init_static_data */
248  NULL, /* encode */
250  NULL, /* decode */
252  NULL, /* flush */
253 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:502
#define NULL
Definition: coverity.c:32
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
float v
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:171
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2745
#define CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:890
int size
Definition: avcodec.h:1163
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3181
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
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:1355
uint8_t * data
Definition: avcodec.h:1162
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2720
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
simple assert() macros that are a bit more flexible than ISO C assert().
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
float y
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:878
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)
float u
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2753
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:341
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1273
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
int extradata_size
Definition: avcodec.h:1356
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1953
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
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:63
common internal api header.
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1604
CCodec * codec
Definition: libutvideo.h:65
void * priv_data
Definition: avcodec.h:1283
#define av_free(p)
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:49
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
#define av_freep(p)
#define MKTAG(a, b, c, d)
Definition: common.h:315
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
AVCodec ff_libutvideo_encoder
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46
bitstream writer API