FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
27 #include "v210enc.h"
28 
29 #define CLIP(v) av_clip(v, 4, 1019)
30 #define CLIP8(v) av_clip(v, 1, 254)
31 
32 #define WRITE_PIXELS(a, b, c) \
33  do { \
34  val = CLIP(*a++); \
35  val |= (CLIP(*b++) << 10) | \
36  (CLIP(*c++) << 20); \
37  AV_WL32(dst, val); \
38  dst += 4; \
39  } while (0)
40 
41 #define WRITE_PIXELS8(a, b, c) \
42  do { \
43  val = (CLIP8(*a++) << 2); \
44  val |= (CLIP8(*b++) << 12) | \
45  (CLIP8(*c++) << 22); \
46  AV_WL32(dst, val); \
47  dst += 4; \
48  } while (0)
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_PIXELS8(u, y, v);
60  WRITE_PIXELS8(y, u, y);
61  WRITE_PIXELS8(v, y, u);
62  WRITE_PIXELS8(y, v, y);
63  WRITE_PIXELS8(u, y, v);
64  WRITE_PIXELS8(y, u, y);
65  WRITE_PIXELS8(v, y, u);
66  WRITE_PIXELS8(y, v, y);
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);
79  WRITE_PIXELS(y, u, y);
80  WRITE_PIXELS(v, y, u);
81  WRITE_PIXELS(y, v, y);
82  }
83 }
84 
86 {
89  s->sample_factor = 1;
90 
91  if (ARCH_X86)
93 }
94 
96 {
97  V210EncContext *s = avctx->priv_data;
98 
99  if (avctx->width & 1) {
100  av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
101  return AVERROR(EINVAL);
102  }
103 
104 #if FF_API_CODED_FRAME
108 #endif
109 
110  ff_v210enc_init(s);
111 
112  return 0;
113 }
114 
116  const AVFrame *pic, int *got_packet)
117 {
118  V210EncContext *s = avctx->priv_data;
119  int aligned_width = ((avctx->width + 47) / 48) * 48;
120  int stride = aligned_width * 8 / 3;
121  int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
122  int h, w, ret;
123  uint8_t *dst;
124 
125  ret = ff_alloc_packet2(avctx, pkt, avctx->height * stride, avctx->height * stride);
126  if (ret < 0) {
127  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
128  return ret;
129  }
130  dst = pkt->data;
131 
132  if (pic->format == AV_PIX_FMT_YUV422P10) {
133  const uint16_t *y = (const uint16_t *)pic->data[0];
134  const uint16_t *u = (const uint16_t *)pic->data[1];
135  const uint16_t *v = (const uint16_t *)pic->data[2];
136  for (h = 0; h < avctx->height; h++) {
137  uint32_t val;
138  w = (avctx->width / (6 * s->sample_factor)) * 6 * s->sample_factor;
139  s->pack_line_10(y, u, v, dst, w);
140 
141  y += w;
142  u += w >> 1;
143  v += w >> 1;
144  dst += (w / (6 * s->sample_factor)) * 16 * s->sample_factor;
145 
146  for (; w < avctx->width - 5; w += 6) {
147  WRITE_PIXELS(u, y, v);
148  WRITE_PIXELS(y, u, y);
149  WRITE_PIXELS(v, y, u);
150  WRITE_PIXELS(y, v, y);
151  }
152  if (w < avctx->width - 1) {
153  WRITE_PIXELS(u, y, v);
154 
155  val = CLIP(*y++);
156  if (w == avctx->width - 2) {
157  AV_WL32(dst, val);
158  dst += 4;
159  }
160  }
161  if (w < avctx->width - 3) {
162  val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
163  AV_WL32(dst, val);
164  dst += 4;
165 
166  val = CLIP(*v++) | (CLIP(*y++) << 10);
167  AV_WL32(dst, val);
168  dst += 4;
169  }
170 
171  memset(dst, 0, line_padding);
172  dst += line_padding;
173  y += pic->linesize[0] / 2 - avctx->width;
174  u += pic->linesize[1] / 2 - avctx->width / 2;
175  v += pic->linesize[2] / 2 - avctx->width / 2;
176  }
177  } else if(pic->format == AV_PIX_FMT_YUV422P) {
178  const uint8_t *y = pic->data[0];
179  const uint8_t *u = pic->data[1];
180  const uint8_t *v = pic->data[2];
181  for (h = 0; h < avctx->height; h++) {
182  uint32_t val;
183  w = (avctx->width / (12 * s->sample_factor)) * 12 * s->sample_factor;
184  s->pack_line_8(y, u, v, dst, w);
185 
186  y += w;
187  u += w >> 1;
188  v += w >> 1;
189  dst += (w / (12 * s->sample_factor)) * 32 * s->sample_factor;
190 
191  for (; w < avctx->width - 5; w += 6) {
192  WRITE_PIXELS8(u, y, v);
193  WRITE_PIXELS8(y, u, y);
194  WRITE_PIXELS8(v, y, u);
195  WRITE_PIXELS8(y, v, y);
196  }
197  if (w < avctx->width - 1) {
198  WRITE_PIXELS8(u, y, v);
199 
200  val = CLIP8(*y++) << 2;
201  if (w == avctx->width - 2) {
202  AV_WL32(dst, val);
203  dst += 4;
204  }
205  }
206  if (w < avctx->width - 3) {
207  val |= (CLIP8(*u++) << 12) | (CLIP8(*y++) << 22);
208  AV_WL32(dst, val);
209  dst += 4;
210 
211  val = (CLIP8(*v++) << 2) | (CLIP8(*y++) << 12);
212  AV_WL32(dst, val);
213  dst += 4;
214  }
215  memset(dst, 0, line_padding);
216  dst += line_padding;
217 
218  y += pic->linesize[0] - avctx->width;
219  u += pic->linesize[1] - avctx->width / 2;
220  v += pic->linesize[2] - avctx->width / 2;
221  }
222  }
223 
224  pkt->flags |= AV_PKT_FLAG_KEY;
225  *got_packet = 1;
226  return 0;
227 }
228 
230  .name = "v210",
231  .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
232  .type = AVMEDIA_TYPE_VIDEO,
233  .id = AV_CODEC_ID_V210,
234  .priv_data_size = sizeof(V210EncContext),
235  .init = encode_init,
236  .encode2 = encode_frame,
238 };
#define WRITE_PIXELS(a, b, c)
Definition: v210enc.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
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
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define CLIP(v)
Definition: v210enc.c:29
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3392
uint8_t
#define av_cold
Definition: attributes.h:82
uint8_t * data
Definition: avcodec.h:1467
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
void(* pack_line_8)(const uint8_t *y, const uint8_t *u, const uint8_t *v, uint8_t *dst, ptrdiff_t width)
Definition: v210enc.h:27
av_cold void ff_v210enc_init(V210EncContext *s)
Definition: v210enc.c:85
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
int width
picture width / height.
Definition: avcodec.h:1711
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet)
Definition: v210enc.c:115
static av_cold int encode_init(AVCodecContext *avctx)
Definition: v210enc.c:95
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:242
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
main external API structure.
Definition: avcodec.h:1532
void ff_v210enc_init_x86(V210EncContext *s)
Definition: v210enc_init.c:36
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1621
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:331
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
#define WRITE_PIXELS8(a, b, c)
Definition: v210enc.c:41
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
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
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2945
void * priv_data
Definition: avcodec.h:1574
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
void(* pack_line_10)(const uint16_t *y, const uint16_t *u, const uint16_t *v, uint8_t *dst, ptrdiff_t width)
Definition: v210enc.h:29
int sample_factor
Definition: v210enc.h:31
AVCodec ff_v210_encoder
Definition: v210enc.c:229
#define CLIP8(v)
Definition: v210enc.c:30
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
for(j=16;j >0;--j)
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
static int width