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 "internal.h"
26 #include "libavutil/bswap.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29 
31 {
32  if (avctx->width & 1) {
33  av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
34  return -1;
35  }
37  avctx->bits_per_raw_sample = 10;
38 
40  if (!avctx->coded_frame)
41  return AVERROR(ENOMEM);
42 
43  return 0;
44 }
45 
46 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
47  AVPacket *avpkt)
48 {
49  int h, w;
50  AVFrame *pic = avctx->coded_frame;
51  const uint8_t *psrc = avpkt->data;
52  uint16_t *y, *u, *v;
53  int aligned_width = ((avctx->width + 47) / 48) * 48;
54  int stride = aligned_width * 8 / 3;
55 
56  if (pic->data[0])
57  avctx->release_buffer(avctx, pic);
58 
59  if (avpkt->size < stride * avctx->height) {
60  av_log(avctx, AV_LOG_ERROR, "packet too small\n");
61  return -1;
62  }
63 
64  pic->reference = 0;
65  if (ff_get_buffer(avctx, pic) < 0)
66  return -1;
67 
68  y = (uint16_t*)pic->data[0];
69  u = (uint16_t*)pic->data[1];
70  v = (uint16_t*)pic->data[2];
72  pic->key_frame = 1;
73 
74 #define READ_PIXELS(a, b, c) \
75  do { \
76  val = av_le2ne32(*src++); \
77  *a++ = val & 0x3FF; \
78  *b++ = (val >> 10) & 0x3FF; \
79  *c++ = (val >> 20) & 0x3FF; \
80  } while (0)
81 
82  for (h = 0; h < avctx->height; h++) {
83  const uint32_t *src = (const uint32_t*)psrc;
84  uint32_t val;
85  for (w = 0; w < avctx->width - 5; w += 6) {
86  READ_PIXELS(u, y, v);
87  READ_PIXELS(y, u, y);
88  READ_PIXELS(v, y, u);
89  READ_PIXELS(y, v, y);
90  }
91  if (w < avctx->width - 1) {
92  READ_PIXELS(u, y, v);
93 
94  val = av_le2ne32(*src++);
95  *y++ = val & 0x3FF;
96  }
97  if (w < avctx->width - 3) {
98  *u++ = (val >> 10) & 0x3FF;
99  *y++ = (val >> 20) & 0x3FF;
100 
101  val = av_le2ne32(*src++);
102  *v++ = val & 0x3FF;
103  *y++ = (val >> 10) & 0x3FF;
104  }
105 
106  psrc += stride;
107  y += pic->linesize[0] / 2 - avctx->width;
108  u += pic->linesize[1] / 2 - avctx->width / 2;
109  v += pic->linesize[2] / 2 - avctx->width / 2;
110  }
111 
112  *got_frame = 1;
113  *(AVFrame*)data = *avctx->coded_frame;
114 
115  return avpkt->size;
116 }
117 
119 {
120  AVFrame *pic = avctx->coded_frame;
121  if (pic->data[0])
122  avctx->release_buffer(avctx, pic);
123  av_freep(&avctx->coded_frame);
124 
125  return 0;
126 }
127 
129  .name = "v210",
130  .type = AVMEDIA_TYPE_VIDEO,
131  .id = AV_CODEC_ID_V210,
132  .init = decode_init,
133  .close = decode_close,
134  .decode = decode_frame,
135  .capabilities = CODEC_CAP_DR1,
136  .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
137 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
memory handling functions
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2711
#define av_le2ne32(x)
Definition: bswap.h:96
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: v210dec.c:46
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
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:151
AVCodec ff_v210_decoder
Definition: v210dec.c:128
uint8_t
static av_cold int decode_init(AVCodecContext *avctx)
Definition: v210dec.c:30
#define b
Definition: input.c:52
const char data[16]
Definition: mxf.c:66
#define READ_PIXELS(a, b, c)
uint8_t * data
Definition: avcodec.h:915
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:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static av_cold int decode_close(AVCodecContext *avctx)
Definition: v210dec.c:118
common internal API header
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static int width
Definition: utils.c:156
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
byte swapping routines
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
This structure stores compressed data.
Definition: avcodec.h:898