FFmpeg
 All Data Structures 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  */
26 
27 extern "C" {
28 #include "libavutil/avassert.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 }
32 
33 #include "libutvideo.h"
34 #include "put_bits.h"
35 
37 {
38  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
39  UtVideoExtra *info;
40  uint32_t flags, in_format;
41 
42  switch (avctx->pix_fmt) {
43  case AV_PIX_FMT_YUV420P:
44  in_format = UTVF_YV12;
45  avctx->bits_per_coded_sample = 12;
46  avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
47  break;
48  case AV_PIX_FMT_YUYV422:
49  in_format = UTVF_YUYV;
50  avctx->bits_per_coded_sample = 16;
51  avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
52  break;
53  case AV_PIX_FMT_BGR24:
54  in_format = UTVF_RGB24_WIN;
55  avctx->bits_per_coded_sample = 24;
56  avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
57  break;
58  case AV_PIX_FMT_RGB32:
59  in_format = UTVF_RGB32_WIN;
60  avctx->bits_per_coded_sample = 32;
61  avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
62  break;
63  default:
64  return AVERROR(EINVAL);
65  }
66 
67  /* Check before we alloc anything */
68  if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
69  av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
70  return AVERROR(EINVAL);
71  }
72 
73  flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
74 
75  avctx->priv_data = utv;
77 
78  /* Alloc extradata buffer */
79  info = (UtVideoExtra *)av_malloc(sizeof(*info));
80 
81  if (info == NULL) {
82  av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
83  return AVERROR(ENOMEM);
84  }
85 
86  /*
87  * We use this buffer to hold the data that Ut Video returns,
88  * since we cannot decode planes separately with it.
89  */
90  utv->buf_size = avpicture_get_size(avctx->pix_fmt,
91  avctx->width, avctx->height);
92  utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
93 
94  if (utv->buffer == NULL) {
95  av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
96  return AVERROR(ENOMEM);
97  }
98 
99  /*
100  * Create a Ut Video instance. Since the function wants
101  * an "interface name" string, pass it the name of the lib.
102  */
103  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
104 
105  /* Initialize encoder */
106  utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
107  CBGROSSWIDTH_WINDOWS);
108 
109  /* Get extradata from encoder */
110  avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
111  utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
112  avctx->width, avctx->height,
113  CBGROSSWIDTH_WINDOWS);
114  avctx->extradata = (uint8_t *)info;
115 
116  /* Set flags */
117  utv->codec->SetState(&flags, sizeof(flags));
118 
119  return 0;
120 }
121 
123  const AVFrame *pic, int *got_packet)
124 {
125  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
126  int w = avctx->width, h = avctx->height;
127  int ret, rgb_size, i;
128  bool keyframe;
129  uint8_t *y, *u, *v;
130  uint8_t *dst;
131 
132  /* Alloc buffer */
133  if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
134  return ret;
135 
136  dst = pkt->data;
137 
138  /* Move input if needed data into Ut Video friendly buffer */
139  switch (avctx->pix_fmt) {
140  case AV_PIX_FMT_YUV420P:
141  y = utv->buffer;
142  u = y + w * h;
143  v = u + w * h / 4;
144  for (i = 0; i < h; i++) {
145  memcpy(y, pic->data[0] + i * pic->linesize[0], w);
146  y += w;
147  }
148  for (i = 0; i < h / 2; i++) {
149  memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
150  memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
151  u += w >> 1;
152  v += w >> 1;
153  }
154  break;
155  case AV_PIX_FMT_YUYV422:
156  for (i = 0; i < h; i++)
157  memcpy(utv->buffer + i * (w << 1),
158  pic->data[0] + i * pic->linesize[0], w << 1);
159  break;
160  case AV_PIX_FMT_BGR24:
161  case AV_PIX_FMT_RGB32:
162  /* Ut Video takes bottom-up BGR */
163  rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
164  for (i = 0; i < h; i++)
165  memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
166  pic->data[0] + i * pic->linesize[0],
167  w * rgb_size);
168  break;
169  default:
170  return AVERROR(EINVAL);
171  }
172 
173  /* Encode frame */
174  pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
175 
176  if (!pkt->size) {
177  av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
178  return AVERROR_INVALIDDATA;
179  }
180 
181  /*
182  * Ut Video is intra-only and every frame is a keyframe,
183  * and the API always returns true. In case something
184  * durastic changes in the future, such as inter support,
185  * assert that this is true.
186  */
187  av_assert2(keyframe == true);
188  avctx->coded_frame->key_frame = 1;
190 
191  pkt->flags |= AV_PKT_FLAG_KEY;
192  *got_packet = 1;
193  return 0;
194 }
195 
197 {
198  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
199 
200  av_freep(&avctx->coded_frame);
201  av_freep(&avctx->extradata);
202  av_freep(&utv->buffer);
203 
204  utv->codec->EncodeEnd();
205  CCodec::DeleteInstance(utv->codec);
206 
207  return 0;
208 }
209 
211  "libutvideo",
212  NULL_IF_CONFIG_SMALL("Ut Video"),
216  NULL, /* supported_framerates */
217  (const enum AVPixelFormat[]) {
220  },
221  NULL, /* supported_samplerates */
222  NULL, /* sample_fmts */
223  NULL, /* channel_layouts */
224  0, /* max_lowres */
225  NULL, /* priv_class */
226  NULL, /* profiles */
227  sizeof(UtVideoContext),
228  NULL, /* next */
229  NULL, /* init_thread_copy */
230  NULL, /* update_thread_context */
231  NULL, /* defaults */
232  NULL, /* init_static_data */
234  NULL, /* encode */
236  NULL, /* decode */
238  NULL, /* flush */
239 };