FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
paf.c
Go to the documentation of this file.
1 /*
2  * Packed Animation File video and audio decoder
3  * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/paf.h"
24 #include "bytestream.h"
25 #include "avcodec.h"
26 #include "copy_block.h"
27 #include "internal.h"
28 
29 
30 static const uint8_t block_sequences[16][8] =
31 {
32  { 0, 0, 0, 0, 0, 0, 0, 0 },
33  { 2, 0, 0, 0, 0, 0, 0, 0 },
34  { 5, 7, 0, 0, 0, 0, 0, 0 },
35  { 5, 0, 0, 0, 0, 0, 0, 0 },
36  { 6, 0, 0, 0, 0, 0, 0, 0 },
37  { 5, 7, 5, 7, 0, 0, 0, 0 },
38  { 5, 7, 5, 0, 0, 0, 0, 0 },
39  { 5, 7, 6, 0, 0, 0, 0, 0 },
40  { 5, 5, 0, 0, 0, 0, 0, 0 },
41  { 3, 0, 0, 0, 0, 0, 0, 0 },
42  { 6, 6, 0, 0, 0, 0, 0, 0 },
43  { 2, 4, 0, 0, 0, 0, 0, 0 },
44  { 2, 4, 5, 7, 0, 0, 0, 0 },
45  { 2, 4, 5, 0, 0, 0, 0, 0 },
46  { 2, 4, 6, 0, 0, 0, 0, 0 },
47  { 2, 4, 5, 7, 5, 7, 0, 0 }
48 };
49 
50 typedef struct PAFVideoDecContext {
53 
58 
61 
63 {
64  PAFVideoDecContext *c = avctx->priv_data;
65  int i;
66 
67  av_frame_free(&c->pic);
68 
69  for (i = 0; i < 4; i++)
70  av_freep(&c->frame[i]);
71 
72  return 0;
73 }
74 
76 {
77  PAFVideoDecContext *c = avctx->priv_data;
78  int i;
79 
80  if (avctx->height & 3 || avctx->width & 3) {
81  av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
82  return AVERROR_INVALIDDATA;
83  }
84 
85  avctx->pix_fmt = AV_PIX_FMT_PAL8;
86 
87  c->pic = av_frame_alloc();
88  if (!c->pic)
89  return AVERROR(ENOMEM);
90 
91  c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
92  c->video_size = avctx->height * avctx->width;
93  for (i = 0; i < 4; i++) {
94  c->frame[i] = av_mallocz(c->frame_size);
95  if (!c->frame[i]) {
96  paf_vid_close(avctx);
97  return AVERROR(ENOMEM);
98  }
99  }
100 
101  return 0;
102 }
103 
105 {
106  int x, y;
107 
108  x = b & 0x7F;
109  y = ((a & 0x3F) << 1) | (b >> 7 & 1);
110 
111  return y * 2 * avctx->width + x * 2;
112 }
113 
114 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
115 {
116  PAFVideoDecContext *c = avctx->priv_data;
117  int i;
118 
119  for (i = 0; i < 4; i++) {
120  bytestream2_get_buffer(&c->gb, dst, 4);
121  dst += avctx->width;
122  }
123 }
124 
126 {
127  int i;
128 
129  for (i = 0; i < 4; i++) {
130  if ((mask >> 4) & (1 << (3 - i)))
131  dst[i] = color;
132  if ((mask & 15) & (1 << (3 - i)))
133  dst[avctx->width + i] = color;
134  }
135 }
136 
137 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
138 {
139  int i;
140 
141  for (i = 0; i < 4; i++) {
142  if ((mask >> 4) & (1 << (3 - i)))
143  dst[i] = src[i];
144  if ((mask & 15) & (1 << (3 - i)))
145  dst[avctx->width + i] = src[avctx->width + i];
146  }
147 }
148 
149 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
150 {
151  PAFVideoDecContext *c = avctx->priv_data;
152  uint32_t opcode_size, offset;
153  uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
154  const uint8_t *src, *send, *opcodes;
155  int i, j, x = 0;
156 
157  i = bytestream2_get_byte(&c->gb);
158  if (i) {
159  if (code & 0x10) {
160  int align;
161 
162  align = bytestream2_tell(&c->gb) & 3;
163  if (align)
164  bytestream2_skip(&c->gb, 4 - align);
165  }
166  do {
167  a = bytestream2_get_byte(&c->gb);
168  b = bytestream2_get_byte(&c->gb);
169  p = (a & 0xC0) >> 6;
170  dst = c->frame[p] + get_video_page_offset(avctx, a, b);
171  dend = c->frame[p] + c->frame_size;
172  offset = (b & 0x7F) * 2;
173  j = bytestream2_get_le16(&c->gb) + offset;
174 
175  do {
176  offset++;
177  if (dst + 3 * avctx->width + 4 > dend)
178  return AVERROR_INVALIDDATA;
179  copy4h(avctx, dst);
180  if ((offset & 0x3F) == 0)
181  dst += avctx->width * 3;
182  dst += 4;
183  } while (offset < j);
184  } while (--i);
185  }
186 
187  dst = c->frame[c->current_frame];
188  dend = c->frame[c->current_frame] + c->frame_size;
189  do {
190  a = bytestream2_get_byte(&c->gb);
191  b = bytestream2_get_byte(&c->gb);
192  p = (a & 0xC0) >> 6;
193  src = c->frame[p] + get_video_page_offset(avctx, a, b);
194  send = c->frame[p] + c->frame_size;
195  if ((src + 3 * avctx->width + 4 > send) ||
196  (dst + 3 * avctx->width + 4 > dend))
197  return AVERROR_INVALIDDATA;
198  copy_block4(dst, src, avctx->width, avctx->width, 4);
199  i++;
200  if ((i & 0x3F) == 0)
201  dst += avctx->width * 3;
202  dst += 4;
203  } while (i < c->video_size / 16);
204 
205  opcode_size = bytestream2_get_le16(&c->gb);
206  bytestream2_skip(&c->gb, 2);
207 
208  if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
209  return AVERROR_INVALIDDATA;
210 
211  opcodes = pkt + bytestream2_tell(&c->gb);
212  bytestream2_skipu(&c->gb, opcode_size);
213 
214  dst = c->frame[c->current_frame];
215 
216  for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
217  for (j = 0; j < avctx->width; j += 4, dst += 4) {
218  int opcode, k = 0;
219 
220  if (x > opcode_size)
221  return AVERROR_INVALIDDATA;
222  if (j & 4) {
223  opcode = opcodes[x] & 15;
224  x++;
225  } else {
226  opcode = opcodes[x] >> 4;
227  }
228 
229  while (block_sequences[opcode][k]) {
230 
231  offset = avctx->width * 2;
232  code = block_sequences[opcode][k++];
233 
234  switch (code) {
235  case 2:
236  offset = 0;
237  case 3:
238  color = bytestream2_get_byte(&c->gb);
239  case 4:
240  mask = bytestream2_get_byte(&c->gb);
241  copy_color_mask(avctx, mask, dst + offset, color);
242  break;
243  case 5:
244  offset = 0;
245  case 6:
246  a = bytestream2_get_byte(&c->gb);
247  b = bytestream2_get_byte(&c->gb);
248  p = (a & 0xC0) >> 6;
249  src = c->frame[p] + get_video_page_offset(avctx, a, b);
250  send = c->frame[p] + c->frame_size;
251  case 7:
252  if (src + offset + avctx->width + 4 > send)
253  return AVERROR_INVALIDDATA;
254  mask = bytestream2_get_byte(&c->gb);
255  copy_src_mask(avctx, mask, dst + offset, src + offset);
256  break;
257  }
258  }
259  }
260  }
261 
262  return 0;
263 }
264 
265 static int paf_vid_decode(AVCodecContext *avctx, void *data,
266  int *got_frame, AVPacket *pkt)
267 {
268  PAFVideoDecContext *c = avctx->priv_data;
269  uint8_t code, *dst, *src, *end;
270  int i, frame, ret;
271 
272  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
273  return ret;
274 
275  bytestream2_init(&c->gb, pkt->data, pkt->size);
276 
277  code = bytestream2_get_byte(&c->gb);
278  if (code & 0x20) {
279  for (i = 0; i < 4; i++)
280  memset(c->frame[i], 0, c->frame_size);
281 
282  memset(c->pic->data[1], 0, AVPALETTE_SIZE);
283  c->current_frame = 0;
284  c->pic->key_frame = 1;
286  } else {
287  c->pic->key_frame = 0;
289  }
290 
291  if (code & 0x40) {
292  uint32_t *out = (uint32_t *)c->pic->data[1];
293  int index, count;
294 
295  index = bytestream2_get_byte(&c->gb);
296  count = bytestream2_get_byte(&c->gb) + 1;
297 
298  if (index + count > 256)
299  return AVERROR_INVALIDDATA;
300  if (bytestream2_get_bytes_left(&c->gb) < 3*count)
301  return AVERROR_INVALIDDATA;
302 
303  out += index;
304  for (i = 0; i < count; i++) {
305  unsigned r, g, b;
306 
307  r = bytestream2_get_byteu(&c->gb);
308  r = r << 2 | r >> 4;
309  g = bytestream2_get_byteu(&c->gb);
310  g = g << 2 | g >> 4;
311  b = bytestream2_get_byteu(&c->gb);
312  b = b << 2 | b >> 4;
313  *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
314  }
315  c->pic->palette_has_changed = 1;
316  }
317 
318  switch (code & 0x0F) {
319  case 0:
320  if ((ret = decode_0(avctx, code, pkt->data)) < 0)
321  return ret;
322  break;
323  case 1:
324  dst = c->frame[c->current_frame];
325  bytestream2_skip(&c->gb, 2);
327  return AVERROR_INVALIDDATA;
328  bytestream2_get_bufferu(&c->gb, dst, c->video_size);
329  break;
330  case 2:
331  frame = bytestream2_get_byte(&c->gb);
332  if (frame > 3)
333  return AVERROR_INVALIDDATA;
334  if (frame != c->current_frame)
335  memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
336  break;
337  case 4:
338  dst = c->frame[c->current_frame];
339  end = dst + c->video_size;
340 
341  bytestream2_skip(&c->gb, 2);
342 
343  while (dst < end) {
344  int8_t code;
345  int count;
346 
347  if (bytestream2_get_bytes_left(&c->gb) < 2)
348  return AVERROR_INVALIDDATA;
349 
350  code = bytestream2_get_byteu(&c->gb);
351  count = FFABS(code) + 1;
352 
353  if (dst + count > end)
354  return AVERROR_INVALIDDATA;
355  if (code < 0)
356  memset(dst, bytestream2_get_byteu(&c->gb), count);
357  else
358  bytestream2_get_buffer(&c->gb, dst, count);
359  dst += count;
360  }
361  break;
362  default:
363  avpriv_request_sample(avctx, "unknown/invalid code");
364  return AVERROR_INVALIDDATA;
365  }
366 
367  dst = c->pic->data[0];
368  src = c->frame[c->current_frame];
369  for (i = 0; i < avctx->height; i++) {
370  memcpy(dst, src, avctx->width);
371  dst += c->pic->linesize[0];
372  src += avctx->width;
373  }
374 
375  c->current_frame = (c->current_frame + 1) & 3;
376  if ((ret = av_frame_ref(data, c->pic)) < 0)
377  return ret;
378 
379  *got_frame = 1;
380 
381  return pkt->size;
382 }
383 
385 {
386  if (avctx->channels != 2) {
387  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
388  return AVERROR_INVALIDDATA;
389  }
390 
392  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
393 
394  return 0;
395 }
396 
397 static int paf_aud_decode(AVCodecContext *avctx, void *data,
398  int *got_frame_ptr, AVPacket *pkt)
399 {
400  AVFrame *frame = data;
401  uint8_t *buf = pkt->data;
402  int16_t *output_samples;
403  const uint8_t *t;
404  int frames, ret, i, j, k;
405 
406  frames = pkt->size / PAF_SOUND_FRAME_SIZE;
407  if (frames < 1)
408  return AVERROR_INVALIDDATA;
409 
410  frame->nb_samples = PAF_SOUND_SAMPLES * frames;
411  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
412  return ret;
413 
414  output_samples = (int16_t *)frame->data[0];
415  for (i = 0; i < frames; i++) {
416  t = buf + 256 * sizeof(uint16_t);
417  for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
418  for (k = 0; k < 2; k++) {
419  *output_samples++ = AV_RL16(buf + *t * 2);
420  t++;
421  }
422  }
423  buf += PAF_SOUND_FRAME_SIZE;
424  }
425 
426  *got_frame_ptr = 1;
427 
428  return pkt->size;
429 }
430 
432  .name = "paf_video",
433  .type = AVMEDIA_TYPE_VIDEO,
434  .id = AV_CODEC_ID_PAF_VIDEO,
435  .priv_data_size = sizeof(PAFVideoDecContext),
436  .init = paf_vid_init,
437  .close = paf_vid_close,
439  .capabilities = CODEC_CAP_DR1,
440  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
441 };
442 
444  .name = "paf_audio",
445  .type = AVMEDIA_TYPE_AUDIO,
446  .id = AV_CODEC_ID_PAF_AUDIO,
447  .init = paf_aud_init,
448  .decode = paf_aud_decode,
449  .capabilities = CODEC_CAP_DR1,
450  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
451 };