jvdec.c
Go to the documentation of this file.
1 /*
2  * Bitmap Brothers JV video decoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "get_bits.h"
31 #include "libavutil/intreadwrite.h"
32 
33 typedef struct JvContext {
38 } JvContext;
39 
41 {
42  JvContext *s = avctx->priv_data;
43  avctx->pix_fmt = PIX_FMT_PAL8;
44  dsputil_init(&s->dsp, avctx);
45 
46  if (!avctx->width || !avctx->height ||
47  (avctx->width & 7) || (avctx->height & 7)) {
48  av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
49  avctx->width, avctx->height);
50  return AVERROR(EINVAL);
51  }
52 
53  return 0;
54 }
55 
59 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
60 {
61  int i, j, v[2];
62 
63  switch (get_bits(gb, 2)) {
64  case 1:
65  v[0] = get_bits(gb, 8);
66  for (j = 0; j < 2; j++)
67  memset(dst + j*linesize, v[0], 2);
68  break;
69  case 2:
70  v[0] = get_bits(gb, 8);
71  v[1] = get_bits(gb, 8);
72  for (j = 0; j < 2; j++)
73  for (i = 0; i < 2; i++)
74  dst[j*linesize + i] = v[get_bits1(gb)];
75  break;
76  case 3:
77  for (j = 0; j < 2; j++)
78  for (i = 0; i < 2; i++)
79  dst[j*linesize + i] = get_bits(gb, 8);
80  }
81 }
82 
86 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
87 {
88  int i, j, v[2];
89 
90  switch (get_bits(gb, 2)) {
91  case 1:
92  v[0] = get_bits(gb, 8);
93  for (j = 0; j < 4; j++)
94  memset(dst + j*linesize, v[0], 4);
95  break;
96  case 2:
97  v[0] = get_bits(gb, 8);
98  v[1] = get_bits(gb, 8);
99  for (j = 2; j >= 0; j -= 2) {
100  for (i = 0; i < 4; i++)
101  dst[j*linesize + i] = v[get_bits1(gb)];
102  for (i = 0; i < 4; i++)
103  dst[(j+1)*linesize + i] = v[get_bits1(gb)];
104  }
105  break;
106  case 3:
107  for (j = 0; j < 4; j += 2)
108  for (i = 0; i < 4; i += 2)
109  decode2x2(gb, dst + j*linesize + i, linesize);
110  }
111 }
112 
116 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
117 {
118  int i, j, v[2];
119 
120  switch (get_bits(gb, 2)) {
121  case 1:
122  v[0] = get_bits(gb, 8);
123  dsp->fill_block_tab[1](dst, v[0], linesize, 8);
124  break;
125  case 2:
126  v[0] = get_bits(gb, 8);
127  v[1] = get_bits(gb, 8);
128  for (j = 7; j >= 0; j--)
129  for (i = 0; i < 8; i++)
130  dst[j*linesize + i] = v[get_bits1(gb)];
131  break;
132  case 3:
133  for (j = 0; j < 8; j += 4)
134  for (i = 0; i < 8; i += 4)
135  decode4x4(gb, dst + j*linesize + i, linesize);
136  }
137 }
138 
139 static int decode_frame(AVCodecContext *avctx,
140  void *data, int *data_size,
141  AVPacket *avpkt)
142 {
143  JvContext *s = avctx->priv_data;
144  int buf_size = avpkt->size;
145  const uint8_t *buf = avpkt->data;
146  const uint8_t *buf_end = buf + buf_size;
147  int video_size, video_type, i, j;
148 
149  video_size = AV_RL32(buf);
150  video_type = buf[4];
151  buf += 5;
152 
153  if (video_size) {
154  if (avctx->reget_buffer(avctx, &s->frame) < 0) {
155  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
156  return -1;
157  }
158 
159  if (video_type == 0 || video_type == 1) {
160  GetBitContext gb;
161  init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf));
162 
163  for (j = 0; j < avctx->height; j += 8)
164  for (i = 0; i < avctx->width; i += 8)
165  decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
166  s->frame.linesize[0], &s->dsp);
167 
168  buf += video_size;
169  } else if (video_type == 2) {
170  if (buf + 1 <= buf_end) {
171  int v = *buf++;
172  for (j = 0; j < avctx->height; j++)
173  memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
174  }
175  } else {
176  av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
177  return AVERROR_INVALIDDATA;
178  }
179  }
180 
181  if (buf < buf_end) {
182  for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
183  s->palette[i] = AV_RB24(buf) << 2;
184  buf += 3;
185  }
186  s->palette_has_changed = 1;
187  }
188 
189  if (video_size) {
190  s->frame.key_frame = 1;
193  s->palette_has_changed = 0;
194  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
195 
196  *data_size = sizeof(AVFrame);
197  *(AVFrame*)data = s->frame;
198  }
199 
200  return buf_size;
201 }
202 
204 {
205  JvContext *s = avctx->priv_data;
206 
207  if(s->frame.data[0])
208  avctx->release_buffer(avctx, &s->frame);
209 
210  return 0;
211 }
212 
214  .name = "jv",
215  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
216  .type = AVMEDIA_TYPE_VIDEO,
217  .id = CODEC_ID_JV,
218  .priv_data_size = sizeof(JvContext),
219  .init = decode_init,
220  .close = decode_close,
221  .decode = decode_frame,
222  .capabilities = CODEC_CAP_DR1,
223 };