FFmpeg  2.1.1
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 
43  switch (avctx->pix_fmt) {
44  case AV_PIX_FMT_YUV420P:
45  in_format = UTVF_YV12;
46  avctx->bits_per_coded_sample = 12;
47  avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
48  break;
49  case AV_PIX_FMT_YUYV422:
50  in_format = UTVF_YUYV;
51  avctx->bits_per_coded_sample = 16;
52  avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
53  break;
54  case AV_PIX_FMT_BGR24:
55  in_format = UTVF_NFCC_BGR_BU;
56  avctx->bits_per_coded_sample = 24;
57  avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
58  break;
59  case AV_PIX_FMT_RGB32:
60  in_format = UTVF_NFCC_BGRA_BU;
61  avctx->bits_per_coded_sample = 32;
62  avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
63  break;
64  default:
65  return AVERROR(EINVAL);
66  }
67 
68  /* Check before we alloc anything */
69  if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
70  av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
71  return AVERROR(EINVAL);
72  }
73 
74  flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
75 
76  avctx->priv_data = utv;
78 
79  /* Alloc extradata buffer */
80  info = (UtVideoExtra *)av_malloc(sizeof(*info));
81 
82  if (info == NULL) {
83  av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
84  return AVERROR(ENOMEM);
85  }
86 
87  /*
88  * We use this buffer to hold the data that Ut Video returns,
89  * since we cannot decode planes separately with it.
90  */
91  utv->buf_size = avpicture_get_size(avctx->pix_fmt,
92  avctx->width, avctx->height);
93  utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
94 
95  if (utv->buffer == NULL) {
96  av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
97  return AVERROR(ENOMEM);
98  }
99 
100  /*
101  * Create a Ut Video instance. Since the function wants
102  * an "interface name" string, pass it the name of the lib.
103  */
104  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
105 
106  /* Initialize encoder */
107  utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
108  CBGROSSWIDTH_WINDOWS);
109 
110  /* Get extradata from encoder */
111  avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
112  utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
113  avctx->width, avctx->height,
114  CBGROSSWIDTH_WINDOWS);
115  avctx->extradata = (uint8_t *)info;
116 
117  /* Set flags */
118  utv->codec->SetState(&flags, sizeof(flags));
119 
120  return 0;
121 }
122 
124  const AVFrame *pic, int *got_packet)
125 {
126  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
127  int w = avctx->width, h = avctx->height;
128  int ret, rgb_size, i;
129  bool keyframe;
130  uint8_t *y, *u, *v;
131  uint8_t *dst;
132 
133  /* Alloc buffer */
134  if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
135  return ret;
136 
137  dst = pkt->data;
138 
139  /* Move input if needed data into Ut Video friendly buffer */
140  switch (avctx->pix_fmt) {
141  case AV_PIX_FMT_YUV420P:
142  y = utv->buffer;
143  u = y + w * h;
144  v = u + w * h / 4;
145  for (i = 0; i < h; i++) {
146  memcpy(y, pic->data[0] + i * pic->linesize[0], w);
147  y += w;
148  }
149  for (i = 0; i < h / 2; i++) {
150  memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
151  memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
152  u += w >> 1;
153  v += w >> 1;
154  }
155  break;
156  case AV_PIX_FMT_YUYV422:
157  for (i = 0; i < h; i++)
158  memcpy(utv->buffer + i * (w << 1),
159  pic->data[0] + i * pic->linesize[0], w << 1);
160  break;
161  case AV_PIX_FMT_BGR24:
162  case AV_PIX_FMT_RGB32:
163  /* Ut Video takes bottom-up BGR */
164  rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
165  for (i = 0; i < h; i++)
166  memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
167  pic->data[0] + i * pic->linesize[0],
168  w * rgb_size);
169  break;
170  default:
171  return AVERROR(EINVAL);
172  }
173 
174  /* Encode frame */
175  pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
176 
177  if (!pkt->size) {
178  av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
179  return AVERROR_INVALIDDATA;
180  }
181 
182  /*
183  * Ut Video is intra-only and every frame is a keyframe,
184  * and the API always returns true. In case something
185  * durastic changes in the future, such as inter support,
186  * assert that this is true.
187  */
188  av_assert2(keyframe == true);
189  avctx->coded_frame->key_frame = 1;
191 
192  pkt->flags |= AV_PKT_FLAG_KEY;
193  *got_packet = 1;
194  return 0;
195 }
196 
198 {
199  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
200 
201  av_freep(&avctx->coded_frame);
202  av_freep(&avctx->extradata);
203  av_freep(&utv->buffer);
204 
205  utv->codec->EncodeEnd();
206  CCodec::DeleteInstance(utv->codec);
207 
208  return 0;
209 }
210 
212  "libutvideo",
213  NULL_IF_CONFIG_SMALL("Ut Video"),
217  NULL, /* supported_framerates */
218  (const enum AVPixelFormat[]) {
221  },
222  NULL, /* supported_samplerates */
223  NULL, /* sample_fmts */
224  NULL, /* channel_layouts */
225  0, /* max_lowres */
226  NULL, /* priv_class */
227  NULL, /* profiles */
228  sizeof(UtVideoContext),
229  NULL, /* next */
230  NULL, /* init_thread_copy */
231  NULL, /* update_thread_context */
232  NULL, /* defaults */
233  NULL, /* init_static_data */
235  NULL, /* encode */
237  NULL, /* decode */
239  NULL, /* flush */
240 };
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1500
float v
static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:1071
#define CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:835
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
Pixel format.
Definition: avcodec.h:4533
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: avcodec.h:4537
uint8_t
uint8_t * buffer
Definition: libutvideo.h:67
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Libavcodec external API header.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
float y
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx-&gt;thread_count == 0 (auto).
Definition: avcodec.h:823
#define AV_PIX_FMT_RGB32
Definition: avcodec.h:4928
Known FOURCCs: &#39;ULY0&#39; (YCbCr 4:2:0), &#39;ULY2&#39; (YCbCr 4:2:2), &#39;ULRG&#39; (RGB), &#39;ULRA&#39; (RGBA), &#39;ULH0&#39; (YCbCr 4:2:0 BT.709), &#39;ULH2&#39; (YCbCr 4:2:2 BT.709)
float u
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2596
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: avcodec.h:4535
main external API structure.
Definition: avcodec.h:1146
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
int extradata_size
Definition: avcodec.h:1255
#define MKTAG(a, b, c, d)
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2588
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
void * priv_data
Definition: avcodec.h:1182
static int flags
Definition: cpu.c:45
unsigned int buf_size
Definition: libutvideo.h:66
common internal api header.
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1498
CCodec * codec
Definition: libutvideo.h:65
#define AVERROR_INVALIDDATA
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 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
#define AVERROR(e)
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
AVCodec ff_libutvideo_encoder
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
bitstream writer API