FFmpeg
v210enc.c
Go to the documentation of this file.
1 /*
2  * V210 encoder
3  *
4  * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
5  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "encode.h"
27 #include "internal.h"
28 #include "v210enc.h"
29 
30 #define TYPE uint8_t
31 #define DEPTH 8
32 #define BYTES_PER_PIXEL 1
33 #define RENAME(a) a ## _ ## 8
34 #include "v210_template.c"
35 #undef RENAME
36 #undef DEPTH
37 #undef BYTES_PER_PIXEL
38 #undef TYPE
39 
40 #define TYPE uint16_t
41 #define DEPTH 10
42 #define BYTES_PER_PIXEL 2
43 #define RENAME(a) a ## _ ## 10
44 #include "v210_template.c"
45 #undef RENAME
46 #undef DEPTH
47 #undef BYTES_PER_PIXEL
48 #undef TYPE
49 
50 static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u,
51  const uint8_t *v, uint8_t *dst,
52  ptrdiff_t width)
53 {
54  uint32_t val;
55  int i;
56 
57  /* unroll this to match the assembly */
58  for (i = 0; i < width - 11; i += 12) {
59  WRITE_PIXELS(u, y, v, 8);
60  WRITE_PIXELS(y, u, y, 8);
61  WRITE_PIXELS(v, y, u, 8);
62  WRITE_PIXELS(y, v, y, 8);
63  WRITE_PIXELS(u, y, v, 8);
64  WRITE_PIXELS(y, u, y, 8);
65  WRITE_PIXELS(v, y, u, 8);
66  WRITE_PIXELS(y, v, y, 8);
67  }
68 }
69 
70 static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u,
71  const uint16_t *v, uint8_t *dst,
72  ptrdiff_t width)
73 {
74  uint32_t val;
75  int i;
76 
77  for (i = 0; i < width - 5; i += 6) {
78  WRITE_PIXELS(u, y, v, 10);
79  WRITE_PIXELS(y, u, y, 10);
80  WRITE_PIXELS(v, y, u, 10);
81  WRITE_PIXELS(y, v, y, 10);
82  }
83 }
84 
86 {
87  s->pack_line_8 = v210_planar_pack_8_c;
88  s->pack_line_10 = v210_planar_pack_10_c;
89  s->sample_factor_8 = 2;
90  s->sample_factor_10 = 1;
91 
92  if (ARCH_X86)
94 }
95 
97 {
98  V210EncContext *s = avctx->priv_data;
99 
100  if (avctx->width & 1) {
101  av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
102  return AVERROR(EINVAL);
103  }
104 
106 
107  avctx->bits_per_coded_sample = 20;
108  avctx->bit_rate = ff_guess_coded_bitrate(avctx) * 16 / 15;
109 
110  return 0;
111 }
112 
114  const AVFrame *pic, int *got_packet)
115 {
116  int aligned_width = ((avctx->width + 47) / 48) * 48;
117  int stride = aligned_width * 8 / 3;
118  AVFrameSideData *side_data;
119  int ret;
120  uint8_t *dst;
121 
122  ret = ff_get_encode_buffer(avctx, pkt, avctx->height * stride, 0);
123  if (ret < 0) {
124  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
125  return ret;
126  }
127  dst = pkt->data;
128 
129  if (pic->format == AV_PIX_FMT_YUV422P10)
130  v210_enc_10(avctx, dst, pic);
131  else if(pic->format == AV_PIX_FMT_YUV422P)
132  v210_enc_8(avctx, dst, pic);
133 
135  if (side_data && side_data->size) {
136  uint8_t *buf = av_packet_new_side_data(pkt, AV_PKT_DATA_A53_CC, side_data->size);
137  if (!buf)
138  return AVERROR(ENOMEM);
139  memcpy(buf, side_data->data, side_data->size);
140  }
141 
142  side_data = av_frame_get_side_data(pic, AV_FRAME_DATA_AFD);
143  if (side_data && side_data->size) {
144  uint8_t *buf = av_packet_new_side_data(pkt, AV_PKT_DATA_AFD, side_data->size);
145  if (!buf)
146  return AVERROR(ENOMEM);
147  memcpy(buf, side_data->data, side_data->size);
148  }
149 
150  *got_packet = 1;
151  return 0;
152 }
153 
155  .name = "v210",
156  .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
157  .type = AVMEDIA_TYPE_VIDEO,
158  .id = AV_CODEC_ID_V210,
160  .priv_data_size = sizeof(V210EncContext),
161  .init = encode_init,
162  .encode2 = encode_frame,
164  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
165 };
ff_guess_coded_bitrate
int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
Get an estimated video bitrate based on frame size, frame rate and coded bits per pixel.
Definition: utils.c:1094
AVCodec
AVCodec.
Definition: codec.h:202
stride
int stride
Definition: mace.c:144
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:617
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
AV_FRAME_DATA_A53_CC
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:58
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
encode.h
AV_PKT_DATA_AFD
@ AV_PKT_DATA_AFD
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition: packet.h:261
init
static int init
Definition: av_tx.c:47
val
static double val(void *priv, double ch)
Definition: aeval.c:76
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVFrameSideData::size
size_t size
Definition: frame.h:226
av_cold
#define av_cold
Definition: attributes.h:90
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
WRITE_PIXELS
#define WRITE_PIXELS(a, b, c, depth)
Definition: v210_template.c:26
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
v210enc.h
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
ff_v210_encoder
const AVCodec ff_v210_encoder
Definition: v210enc.c:154
V210EncContext
Definition: v210enc.h:26
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
Definition: v210enc.c:113
v210_template.c
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:405
ff_v210enc_init
av_cold void ff_v210enc_init(V210EncContext *s)
Definition: v210enc.c:85
AV_FRAME_DATA_AFD
@ AV_FRAME_DATA_AFD
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition: frame.h:89
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_CODEC_ID_V210
@ AV_CODEC_ID_V210
Definition: codec_id.h:177
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: v210enc.c:96
AVFrameSideData::data
uint8_t * data
Definition: frame.h:225
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:404
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1418
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AV_PKT_DATA_A53_CC
@ AV_PKT_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: packet.h:242
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
ff_v210enc_init_x86
void ff_v210enc_init_x86(V210EncContext *s)
Definition: v210enc_init.c:37
AVCodecContext::height
int height
Definition: avcodec.h:556
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:383
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:232
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:78
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
v210_planar_pack_10_c
static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u, const uint16_t *v, uint8_t *dst, ptrdiff_t width)
Definition: v210enc.c:70
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:223
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
v210_planar_pack_8_c
static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u, const uint8_t *v, uint8_t *dst, ptrdiff_t width)
Definition: v210enc.c:50