FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vcr1.c
Go to the documentation of this file.
1 /*
2  * ATI VCR1 codec
3  * Copyright (c) 2003 Michael Niedermayer
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * ATI VCR1 codec
25  */
26 
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "internal.h"
30 #include "libavutil/internal.h"
31 
32 typedef struct VCR1Context {
34  int delta[16];
35  int offset[4];
36 } VCR1Context;
37 
39 {
40  VCR1Context *const a = avctx->priv_data;
41 
42  avctx->coded_frame = &a->picture;
44 
45  return 0;
46 }
47 
49 {
50  vcr1_common_init(avctx);
51 
52  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
53 
54  if (avctx->width % 8 || avctx->height%4) {
55  av_log_ask_for_sample(avctx, "odd dimensions are not supported\n");
56  return AVERROR_PATCHWELCOME;
57  }
58  return 0;
59 }
60 
62 {
63  VCR1Context *s = avctx->priv_data;
64 
65  if (s->picture.data[0])
66  avctx->release_buffer(avctx, &s->picture);
67 
68  return 0;
69 }
70 
71 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
72  int *got_frame, AVPacket *avpkt)
73 {
74  const uint8_t *buf = avpkt->data;
75  int buf_size = avpkt->size;
76  VCR1Context *const a = avctx->priv_data;
77  AVFrame *picture = data;
78  AVFrame *const p = &a->picture;
79  const uint8_t *bytestream = buf;
80  int i, x, y;
81 
82  if (p->data[0])
83  avctx->release_buffer(avctx, p);
84 
85  if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
86  av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
87  return AVERROR(EINVAL);
88  }
89 
90  p->reference = 0;
91  if (ff_get_buffer(avctx, p) < 0) {
92  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
93  return -1;
94  }
96  p->key_frame = 1;
97 
98  for (i = 0; i < 16; i++) {
99  a->delta[i] = *bytestream++;
100  bytestream++;
101  }
102 
103  for (y = 0; y < avctx->height; y++) {
104  int offset;
105  uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
106 
107  if ((y & 3) == 0) {
108  uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
109  uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
110 
111  for (i = 0; i < 4; i++)
112  a->offset[i] = *bytestream++;
113 
114  offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
115  for (x = 0; x < avctx->width; x += 4) {
116  luma[0] = offset += a->delta[bytestream[2] & 0xF];
117  luma[1] = offset += a->delta[bytestream[2] >> 4];
118  luma[2] = offset += a->delta[bytestream[0] & 0xF];
119  luma[3] = offset += a->delta[bytestream[0] >> 4];
120  luma += 4;
121 
122  *cb++ = bytestream[3];
123  *cr++ = bytestream[1];
124 
125  bytestream += 4;
126  }
127  } else {
128  offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
129 
130  for (x = 0; x < avctx->width; x += 8) {
131  luma[0] = offset += a->delta[bytestream[2] & 0xF];
132  luma[1] = offset += a->delta[bytestream[2] >> 4];
133  luma[2] = offset += a->delta[bytestream[3] & 0xF];
134  luma[3] = offset += a->delta[bytestream[3] >> 4];
135  luma[4] = offset += a->delta[bytestream[0] & 0xF];
136  luma[5] = offset += a->delta[bytestream[0] >> 4];
137  luma[6] = offset += a->delta[bytestream[1] & 0xF];
138  luma[7] = offset += a->delta[bytestream[1] >> 4];
139  luma += 8;
140  bytestream += 4;
141  }
142  }
143  }
144 
145  *picture = a->picture;
146  *got_frame = 1;
147 
148  return buf_size;
149 }
150 
152  .name = "vcr1",
153  .type = AVMEDIA_TYPE_VIDEO,
154  .id = AV_CODEC_ID_VCR1,
155  .priv_data_size = sizeof(VCR1Context),
159  .capabilities = CODEC_CAP_DR1,
160  .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
161 };
162 
163 /* Disable the encoder. */
164 #undef CONFIG_VCR1_ENCODER
165 #define CONFIG_VCR1_ENCODER 0
166 
167 #if CONFIG_VCR1_ENCODER
168 
169 #include "put_bits.h"
170 
171 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
172  int buf_size, void *data)
173 {
174  VCR1Context *const a = avctx->priv_data;
175  AVFrame *pict = data;
176  AVFrame *const p = &a->picture;
177  int size;
178 
179  *p = *pict;
181  p->key_frame = 1;
182 
183  avpriv_align_put_bits(&a->pb);
184  flush_put_bits(&a->pb);
185 
186  size = put_bits_count(&a->pb) / 32;
187 
188  return size * 4;
189 }
190 
191 AVCodec ff_vcr1_encoder = {
192  .name = "vcr1",
193  .type = AVMEDIA_TYPE_VIDEO,
194  .id = AV_CODEC_ID_VCR1,
195  .priv_data_size = sizeof(VCR1Context),
197  .encode = vcr1_encode_frame,
198  .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
199 };
200 #endif /* CONFIG_VCR1_ENCODER */