v210dec.c
Go to the documentation of this file.
1 /*
2  * V210 decoder
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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 "libavutil/bswap.h"
26 
28 {
29  if (avctx->width & 1) {
30  av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
31  return -1;
32  }
33  avctx->pix_fmt = PIX_FMT_YUV422P10;
34  avctx->bits_per_raw_sample = 10;
35 
37 
38  return 0;
39 }
40 
41 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
42  AVPacket *avpkt)
43 {
44  int h, w;
45  AVFrame *pic = avctx->coded_frame;
46  const uint8_t *psrc = avpkt->data;
47  uint16_t *y, *u, *v;
48  int aligned_width = ((avctx->width + 47) / 48) * 48;
49  int stride = aligned_width * 8 / 3;
50 
51  if (pic->data[0])
52  avctx->release_buffer(avctx, pic);
53 
54  if (avpkt->size < stride * avctx->height) {
55  av_log(avctx, AV_LOG_ERROR, "packet too small\n");
56  return -1;
57  }
58 
59  pic->reference = 0;
60  if (avctx->get_buffer(avctx, pic) < 0)
61  return -1;
62 
63  y = (uint16_t*)pic->data[0];
64  u = (uint16_t*)pic->data[1];
65  v = (uint16_t*)pic->data[2];
67  pic->key_frame = 1;
68 
69 #define READ_PIXELS(a, b, c) \
70  do { \
71  val = av_le2ne32(*src++); \
72  *a++ = val & 0x3FF; \
73  *b++ = (val >> 10) & 0x3FF; \
74  *c++ = (val >> 20) & 0x3FF; \
75  } while (0)
76 
77  for (h = 0; h < avctx->height; h++) {
78  const uint32_t *src = (const uint32_t*)psrc;
79  uint32_t val;
80  for (w = 0; w < avctx->width - 5; w += 6) {
81  READ_PIXELS(u, y, v);
82  READ_PIXELS(y, u, y);
83  READ_PIXELS(v, y, u);
84  READ_PIXELS(y, v, y);
85  }
86  if (w < avctx->width - 1) {
87  READ_PIXELS(u, y, v);
88 
89  val = av_le2ne32(*src++);
90  *y++ = val & 0x3FF;
91  }
92  if (w < avctx->width - 3) {
93  *u++ = (val >> 10) & 0x3FF;
94  *y++ = (val >> 20) & 0x3FF;
95 
96  val = av_le2ne32(*src++);
97  *v++ = val & 0x3FF;
98  *y++ = (val >> 10) & 0x3FF;
99  }
100 
101  psrc += stride;
102  y += pic->linesize[0] / 2 - avctx->width;
103  u += pic->linesize[1] / 2 - avctx->width / 2;
104  v += pic->linesize[2] / 2 - avctx->width / 2;
105  }
106 
107  *data_size = sizeof(AVFrame);
108  *(AVFrame*)data = *avctx->coded_frame;
109 
110  return avpkt->size;
111 }
112 
114 {
115  AVFrame *pic = avctx->coded_frame;
116  if (pic->data[0])
117  avctx->release_buffer(avctx, pic);
118  av_freep(&avctx->coded_frame);
119 
120  return 0;
121 }
122 
124  .name = "v210",
125  .type = AVMEDIA_TYPE_VIDEO,
126  .id = CODEC_ID_V210,
127  .init = decode_init,
128  .close = decode_close,
129  .decode = decode_frame,
130  .capabilities = CODEC_CAP_DR1,
131  .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
132 };
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
int size
Definition: avcodec.h:909
int width
Definition: rotozoom.c:164
#define b
Definition: swscale.c:1335
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2863
#define av_le2ne32(x)
Definition: bswap.h:96
int stride
Definition: mace.c:143
AVCodec.
Definition: avcodec.h:3189
#define v(n)
Definition: regs.h:34
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:147
AVCodec ff_v210_decoder
Definition: v210dec.c:123
#define av_cold
Definition: attributes.h:71
static av_cold int decode_init(AVCodecContext *avctx)
Definition: v210dec.c:27
const char data[16]
Definition: mxf.c:60
#define READ_PIXELS(a, b, c)
uint8_t * data
Definition: avcodec.h:908
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
static av_cold int decode_close(AVCodecContext *avctx)
Definition: v210dec.c:113
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: v210dec.c:41
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
#define PIX_FMT_YUV422P10
Definition: pixfmt.h:188
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
byte swapping routines
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022