lavc: add a wrapper for AVCodecContext.get_buffer().
[libav.git] / libavcodec / v210x.c
1 /*
2 * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "avcodec.h"
22 #include "internal.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/mem.h"
26
27 static av_cold int decode_init(AVCodecContext *avctx)
28 {
29 if(avctx->width & 1){
30 av_log(avctx, AV_LOG_ERROR, "v210x needs even width\n");
31 return -1;
32 }
33 avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
34 avctx->bits_per_raw_sample= 10;
35
36 avctx->coded_frame= avcodec_alloc_frame();
37
38 return 0;
39 }
40
41 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
42 {
43 int y=0;
44 int width= avctx->width;
45 AVFrame *pic= avctx->coded_frame;
46 const uint32_t *src= (const uint32_t *)avpkt->data;
47 uint16_t *ydst, *udst, *vdst, *yend;
48
49 if(pic->data[0])
50 avctx->release_buffer(avctx, pic);
51
52 if(avpkt->size < avctx->width * avctx->height * 8 / 3){
53 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
54 return -1;
55 }
56
57 if(avpkt->size > avctx->width * avctx->height * 8 / 3){
58 av_log_ask_for_sample(avctx, "Probably padded data\n");
59 }
60
61 pic->reference= 0;
62 if(ff_get_buffer(avctx, pic) < 0)
63 return -1;
64
65 ydst= (uint16_t *)pic->data[0];
66 udst= (uint16_t *)pic->data[1];
67 vdst= (uint16_t *)pic->data[2];
68 yend= ydst + width;
69 pic->pict_type= AV_PICTURE_TYPE_I;
70 pic->key_frame= 1;
71
72 for(;;){
73 uint32_t v= av_be2ne32(*src++);
74 *udst++= (v>>16) & 0xFFC0;
75 *ydst++= (v>>6 ) & 0xFFC0;
76 *vdst++= (v<<4 ) & 0xFFC0;
77
78 v= av_be2ne32(*src++);
79 *ydst++= (v>>16) & 0xFFC0;
80
81 if(ydst >= yend){
82 ydst+= pic->linesize[0]/2 - width;
83 udst+= pic->linesize[1]/2 - width/2;
84 vdst+= pic->linesize[2]/2 - width/2;
85 yend= ydst + width;
86 if(++y >= avctx->height)
87 break;
88 }
89
90 *udst++= (v>>6 ) & 0xFFC0;
91 *ydst++= (v<<4 ) & 0xFFC0;
92
93 v= av_be2ne32(*src++);
94 *vdst++= (v>>16) & 0xFFC0;
95 *ydst++= (v>>6 ) & 0xFFC0;
96
97 if(ydst >= yend){
98 ydst+= pic->linesize[0]/2 - width;
99 udst+= pic->linesize[1]/2 - width/2;
100 vdst+= pic->linesize[2]/2 - width/2;
101 yend= ydst + width;
102 if(++y >= avctx->height)
103 break;
104 }
105
106 *udst++= (v<<4 ) & 0xFFC0;
107
108 v= av_be2ne32(*src++);
109 *ydst++= (v>>16) & 0xFFC0;
110 *vdst++= (v>>6 ) & 0xFFC0;
111 *ydst++= (v<<4 ) & 0xFFC0;
112 if(ydst >= yend){
113 ydst+= pic->linesize[0]/2 - width;
114 udst+= pic->linesize[1]/2 - width/2;
115 vdst+= pic->linesize[2]/2 - width/2;
116 yend= ydst + width;
117 if(++y >= avctx->height)
118 break;
119 }
120 }
121
122 *data_size=sizeof(AVFrame);
123 *(AVFrame*)data= *avctx->coded_frame;
124
125 return avpkt->size;
126 }
127
128 static av_cold int decode_close(AVCodecContext *avctx)
129 {
130 AVFrame *pic = avctx->coded_frame;
131 if (pic->data[0])
132 avctx->release_buffer(avctx, pic);
133 av_freep(&avctx->coded_frame);
134
135 return 0;
136 }
137
138 AVCodec ff_v210x_decoder = {
139 .name = "v210x",
140 .type = AVMEDIA_TYPE_VIDEO,
141 .id = AV_CODEC_ID_V210X,
142 .init = decode_init,
143 .close = decode_close,
144 .decode = decode_frame,
145 .capabilities = CODEC_CAP_DR1,
146 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
147 };