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;
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_NFCC_BGR_BU;
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_NFCC_BGRA_BU;
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 };
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:76
AVFrame * coded_frame
the picture in the bitstream
#define CODEC_CAP_LOSSLESS
Codec is lossless.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
output residual component w
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
uint8_t
#define av_cold
Definition: attributes.h:78
uint8_t * buffer
Definition: libutvideo.h:58
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t * data
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
external API header
int flags
A combination of AV_PKT_FLAG values.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
MIPS optimizations info
Definition: mips.txt:2
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Known FOURCCs: &#39;ULY0&#39; (YCbCr 4:2:0), &#39;ULY2&#39; (YCbCr 4:2:2), &#39;ULRG&#39; (RGB), &#39;ULRA&#39; (RGBA) ...
float u
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:41
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:69
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
unsigned int buf_size
Definition: libutvideo.h:57
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
common internal api header.
int prediction_method
prediction method (needed for huffyuv)
function y
Definition: D.m:1
CCodec * codec
Definition: libutvideo.h:56
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
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:139
#define MKTAG(a, b, c, d)
Definition: common.h:282
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
AVCodec ff_libutvideo_encoder
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:45
bitstream writer API