FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
interplayvideo.c
Go to the documentation of this file.
1 /*
2  * Interplay MVE Video Decoder
3  * Copyright (C) 2003 The FFmpeg project
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  * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
25  * For more information about the Interplay MVE format, visit:
26  * http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
27  * This code is written in such a way that the identifiers match up
28  * with the encoding descriptions in the document.
29  *
30  * This decoder presently only supports a PAL8 output colorspace.
31  *
32  * An Interplay video frame consists of 2 parts: The decoding map and
33  * the video data. A demuxer must load these 2 parts together in a single
34  * buffer before sending it through the stream to this decoder.
35  */
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "libavutil/intreadwrite.h"
42 
43 #define BITSTREAM_READER_LE
44 #include "avcodec.h"
45 #include "bytestream.h"
46 #include "get_bits.h"
47 #include "hpeldsp.h"
48 #include "internal.h"
49 
50 #define PALETTE_COUNT 256
51 
52 typedef struct IpvideoContext {
53 
58  const unsigned char *decoding_map;
60 
61  int is_16bpp;
63  unsigned char *pixel_ptr;
64  int line_inc;
65  int stride;
67 
68  uint32_t pal[256];
70 
71 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
72 {
73  int current_offset = s->pixel_ptr - dst->data[0];
74  int motion_offset = current_offset + delta_y * dst->linesize[0]
75  + delta_x * (1 + s->is_16bpp);
76  if (motion_offset < 0) {
77  av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
78  return AVERROR_INVALIDDATA;
79  } else if (motion_offset > s->upper_motion_limit_offset) {
80  av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
81  motion_offset, s->upper_motion_limit_offset);
82  return AVERROR_INVALIDDATA;
83  }
84  if (!src->data[0]) {
85  av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
86  return AVERROR(EINVAL);
87  }
88  s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
89  dst->linesize[0], 8);
90  return 0;
91 }
92 
94 {
95  return copy_from(s, s->last_frame, frame, 0, 0);
96 }
97 
99 {
100  return copy_from(s, s->second_last_frame, frame, 0, 0);
101 }
102 
104 {
105  unsigned char B;
106  int x, y;
107 
108  /* copy block from 2 frames ago using a motion vector; need 1 more byte */
109  if (!s->is_16bpp) {
110  B = bytestream2_get_byte(&s->stream_ptr);
111  } else {
112  B = bytestream2_get_byte(&s->mv_ptr);
113  }
114 
115  if (B < 56) {
116  x = 8 + (B % 7);
117  y = B / 7;
118  } else {
119  x = -14 + ((B - 56) % 29);
120  y = 8 + ((B - 56) / 29);
121  }
122 
123  ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
124  return copy_from(s, s->second_last_frame, frame, x, y);
125 }
126 
128 {
129  unsigned char B;
130  int x, y;
131 
132  /* copy 8x8 block from current frame from an up/left block */
133 
134  /* need 1 more byte for motion */
135  if (!s->is_16bpp) {
136  B = bytestream2_get_byte(&s->stream_ptr);
137  } else {
138  B = bytestream2_get_byte(&s->mv_ptr);
139  }
140 
141  if (B < 56) {
142  x = -(8 + (B % 7));
143  y = -(B / 7);
144  } else {
145  x = -(-14 + ((B - 56) % 29));
146  y = -( 8 + ((B - 56) / 29));
147  }
148 
149  ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
150  return copy_from(s, frame, frame, x, y);
151 }
152 
154 {
155  int x, y;
156  unsigned char B, BL, BH;
157 
158  /* copy a block from the previous frame; need 1 more byte */
159  if (!s->is_16bpp) {
160  B = bytestream2_get_byte(&s->stream_ptr);
161  } else {
162  B = bytestream2_get_byte(&s->mv_ptr);
163  }
164 
165  BL = B & 0x0F;
166  BH = (B >> 4) & 0x0F;
167  x = -8 + BL;
168  y = -8 + BH;
169 
170  ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
171  return copy_from(s, s->last_frame, frame, x, y);
172 }
173 
175 {
176  signed char x, y;
177 
178  /* copy a block from the previous frame using an expanded range;
179  * need 2 more bytes */
180  x = bytestream2_get_byte(&s->stream_ptr);
181  y = bytestream2_get_byte(&s->stream_ptr);
182 
183  ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
184  return copy_from(s, s->last_frame, frame, x, y);
185 }
186 
188 {
189  /* mystery opcode? skip multiple blocks? */
190  av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
191 
192  /* report success */
193  return 0;
194 }
195 
197 {
198  int x, y;
199  unsigned char P[2];
200  unsigned int flags;
201 
202  if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
203  av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
204  return AVERROR_INVALIDDATA;
205  }
206 
207  /* 2-color encoding */
208  P[0] = bytestream2_get_byte(&s->stream_ptr);
209  P[1] = bytestream2_get_byte(&s->stream_ptr);
210 
211  if (P[0] <= P[1]) {
212 
213  /* need 8 more bytes from the stream */
214  for (y = 0; y < 8; y++) {
215  flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
216  for (; flags != 1; flags >>= 1)
217  *s->pixel_ptr++ = P[flags & 1];
218  s->pixel_ptr += s->line_inc;
219  }
220 
221  } else {
222 
223  /* need 2 more bytes from the stream */
224  flags = bytestream2_get_le16(&s->stream_ptr);
225  for (y = 0; y < 8; y += 2) {
226  for (x = 0; x < 8; x += 2, flags >>= 1) {
227  s->pixel_ptr[x ] =
228  s->pixel_ptr[x + 1 ] =
229  s->pixel_ptr[x + s->stride] =
230  s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
231  }
232  s->pixel_ptr += s->stride * 2;
233  }
234  }
235 
236  /* report success */
237  return 0;
238 }
239 
241 {
242  int x, y;
243  unsigned char P[4];
244  unsigned int flags = 0;
245 
246  if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
247  av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
248  return AVERROR_INVALIDDATA;
249  }
250 
251  /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
252  * either top and bottom or left and right halves */
253  P[0] = bytestream2_get_byte(&s->stream_ptr);
254  P[1] = bytestream2_get_byte(&s->stream_ptr);
255 
256  if (P[0] <= P[1]) {
257  for (y = 0; y < 16; y++) {
258  // new values for each 4x4 block
259  if (!(y & 3)) {
260  if (y) {
261  P[0] = bytestream2_get_byte(&s->stream_ptr);
262  P[1] = bytestream2_get_byte(&s->stream_ptr);
263  }
264  flags = bytestream2_get_le16(&s->stream_ptr);
265  }
266 
267  for (x = 0; x < 4; x++, flags >>= 1)
268  *s->pixel_ptr++ = P[flags & 1];
269  s->pixel_ptr += s->stride - 4;
270  // switch to right half
271  if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
272  }
273 
274  } else {
275  flags = bytestream2_get_le32(&s->stream_ptr);
276  P[2] = bytestream2_get_byte(&s->stream_ptr);
277  P[3] = bytestream2_get_byte(&s->stream_ptr);
278 
279  if (P[2] <= P[3]) {
280 
281  /* vertical split; left & right halves are 2-color encoded */
282 
283  for (y = 0; y < 16; y++) {
284  for (x = 0; x < 4; x++, flags >>= 1)
285  *s->pixel_ptr++ = P[flags & 1];
286  s->pixel_ptr += s->stride - 4;
287  // switch to right half
288  if (y == 7) {
289  s->pixel_ptr -= 8 * s->stride - 4;
290  P[0] = P[2];
291  P[1] = P[3];
292  flags = bytestream2_get_le32(&s->stream_ptr);
293  }
294  }
295 
296  } else {
297 
298  /* horizontal split; top & bottom halves are 2-color encoded */
299 
300  for (y = 0; y < 8; y++) {
301  if (y == 4) {
302  P[0] = P[2];
303  P[1] = P[3];
304  flags = bytestream2_get_le32(&s->stream_ptr);
305  }
306 
307  for (x = 0; x < 8; x++, flags >>= 1)
308  *s->pixel_ptr++ = P[flags & 1];
309  s->pixel_ptr += s->line_inc;
310  }
311  }
312  }
313 
314  /* report success */
315  return 0;
316 }
317 
319 {
320  int x, y;
321  unsigned char P[4];
322 
323  if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
324  av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
325  return AVERROR_INVALIDDATA;
326  }
327 
328  /* 4-color encoding */
330 
331  if (P[0] <= P[1]) {
332  if (P[2] <= P[3]) {
333 
334  /* 1 of 4 colors for each pixel, need 16 more bytes */
335  for (y = 0; y < 8; y++) {
336  /* get the next set of 8 2-bit flags */
337  int flags = bytestream2_get_le16(&s->stream_ptr);
338  for (x = 0; x < 8; x++, flags >>= 2)
339  *s->pixel_ptr++ = P[flags & 0x03];
340  s->pixel_ptr += s->line_inc;
341  }
342 
343  } else {
344  uint32_t flags;
345 
346  /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
347  flags = bytestream2_get_le32(&s->stream_ptr);
348 
349  for (y = 0; y < 8; y += 2) {
350  for (x = 0; x < 8; x += 2, flags >>= 2) {
351  s->pixel_ptr[x ] =
352  s->pixel_ptr[x + 1 ] =
353  s->pixel_ptr[x + s->stride] =
354  s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
355  }
356  s->pixel_ptr += s->stride * 2;
357  }
358 
359  }
360  } else {
361  uint64_t flags;
362 
363  /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
364  flags = bytestream2_get_le64(&s->stream_ptr);
365  if (P[2] <= P[3]) {
366  for (y = 0; y < 8; y++) {
367  for (x = 0; x < 8; x += 2, flags >>= 2) {
368  s->pixel_ptr[x ] =
369  s->pixel_ptr[x + 1] = P[flags & 0x03];
370  }
371  s->pixel_ptr += s->stride;
372  }
373  } else {
374  for (y = 0; y < 8; y += 2) {
375  for (x = 0; x < 8; x++, flags >>= 2) {
376  s->pixel_ptr[x ] =
377  s->pixel_ptr[x + s->stride] = P[flags & 0x03];
378  }
379  s->pixel_ptr += s->stride * 2;
380  }
381  }
382  }
383 
384  /* report success */
385  return 0;
386 }
387 
389 {
390  int x, y;
391  unsigned char P[8];
392  int flags = 0;
393 
394  if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
395  av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
396  return AVERROR_INVALIDDATA;
397  }
398 
400 
401  /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
402  * either top and bottom or left and right halves */
403  if (P[0] <= P[1]) {
404 
405  /* 4-color encoding for each quadrant; need 32 bytes */
406  for (y = 0; y < 16; y++) {
407  // new values for each 4x4 block
408  if (!(y & 3)) {
409  if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
410  flags = bytestream2_get_le32(&s->stream_ptr);
411  }
412 
413  for (x = 0; x < 4; x++, flags >>= 2)
414  *s->pixel_ptr++ = P[flags & 0x03];
415 
416  s->pixel_ptr += s->stride - 4;
417  // switch to right half
418  if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
419  }
420 
421  } else {
422  // vertical split?
423  int vert;
424  uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
425 
426  bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
427  vert = P[4] <= P[5];
428 
429  /* 4-color encoding for either left and right or top and bottom
430  * halves */
431 
432  for (y = 0; y < 16; y++) {
433  for (x = 0; x < 4; x++, flags >>= 2)
434  *s->pixel_ptr++ = P[flags & 0x03];
435 
436  if (vert) {
437  s->pixel_ptr += s->stride - 4;
438  // switch to right half
439  if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
440  } else if (y & 1) s->pixel_ptr += s->line_inc;
441 
442  // load values for second half
443  if (y == 7) {
444  memcpy(P, P + 4, 4);
445  flags = bytestream2_get_le64(&s->stream_ptr);
446  }
447  }
448  }
449 
450  /* report success */
451  return 0;
452 }
453 
455 {
456  int y;
457 
458  /* 64-color encoding (each pixel in block is a different color) */
459  for (y = 0; y < 8; y++) {
461  s->pixel_ptr += s->stride;
462  }
463 
464  /* report success */
465  return 0;
466 }
467 
469 {
470  int x, y;
471 
472  /* 16-color block encoding: each 2x2 block is a different color */
473  for (y = 0; y < 8; y += 2) {
474  for (x = 0; x < 8; x += 2) {
475  s->pixel_ptr[x ] =
476  s->pixel_ptr[x + 1 ] =
477  s->pixel_ptr[x + s->stride] =
478  s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
479  }
480  s->pixel_ptr += s->stride * 2;
481  }
482 
483  /* report success */
484  return 0;
485 }
486 
488 {
489  int y;
490  unsigned char P[2];
491 
492  if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
493  av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
494  return AVERROR_INVALIDDATA;
495  }
496 
497  /* 4-color block encoding: each 4x4 block is a different color */
498  for (y = 0; y < 8; y++) {
499  if (!(y & 3)) {
500  P[0] = bytestream2_get_byte(&s->stream_ptr);
501  P[1] = bytestream2_get_byte(&s->stream_ptr);
502  }
503  memset(s->pixel_ptr, P[0], 4);
504  memset(s->pixel_ptr + 4, P[1], 4);
505  s->pixel_ptr += s->stride;
506  }
507 
508  /* report success */
509  return 0;
510 }
511 
513 {
514  int y;
515  unsigned char pix;
516 
517  /* 1-color encoding: the whole block is 1 solid color */
518  pix = bytestream2_get_byte(&s->stream_ptr);
519 
520  for (y = 0; y < 8; y++) {
521  memset(s->pixel_ptr, pix, 8);
522  s->pixel_ptr += s->stride;
523  }
524 
525  /* report success */
526  return 0;
527 }
528 
530 {
531  int x, y;
532  unsigned char sample[2];
533 
534  /* dithered encoding */
535  sample[0] = bytestream2_get_byte(&s->stream_ptr);
536  sample[1] = bytestream2_get_byte(&s->stream_ptr);
537 
538  for (y = 0; y < 8; y++) {
539  for (x = 0; x < 8; x += 2) {
540  *s->pixel_ptr++ = sample[ y & 1 ];
541  *s->pixel_ptr++ = sample[!(y & 1)];
542  }
543  s->pixel_ptr += s->line_inc;
544  }
545 
546  /* report success */
547  return 0;
548 }
549 
551 {
552  signed char x, y;
553 
554  /* copy a block from the second last frame using an expanded range */
555  x = bytestream2_get_byte(&s->stream_ptr);
556  y = bytestream2_get_byte(&s->stream_ptr);
557 
558  ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
559  return copy_from(s, s->second_last_frame, frame, x, y);
560 }
561 
563 {
564  int x, y;
565  uint16_t P[2];
566  unsigned int flags;
567  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
568 
569  /* 2-color encoding */
570  P[0] = bytestream2_get_le16(&s->stream_ptr);
571  P[1] = bytestream2_get_le16(&s->stream_ptr);
572 
573  if (!(P[0] & 0x8000)) {
574 
575  for (y = 0; y < 8; y++) {
576  flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
577  for (; flags != 1; flags >>= 1)
578  *pixel_ptr++ = P[flags & 1];
579  pixel_ptr += s->line_inc;
580  }
581 
582  } else {
583 
584  flags = bytestream2_get_le16(&s->stream_ptr);
585  for (y = 0; y < 8; y += 2) {
586  for (x = 0; x < 8; x += 2, flags >>= 1) {
587  pixel_ptr[x ] =
588  pixel_ptr[x + 1 ] =
589  pixel_ptr[x + s->stride] =
590  pixel_ptr[x + 1 + s->stride] = P[flags & 1];
591  }
592  pixel_ptr += s->stride * 2;
593  }
594  }
595 
596  return 0;
597 }
598 
600 {
601  int x, y;
602  uint16_t P[4];
603  unsigned int flags = 0;
604  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
605 
606  /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
607  * either top and bottom or left and right halves */
608  P[0] = bytestream2_get_le16(&s->stream_ptr);
609  P[1] = bytestream2_get_le16(&s->stream_ptr);
610 
611  if (!(P[0] & 0x8000)) {
612 
613  for (y = 0; y < 16; y++) {
614  // new values for each 4x4 block
615  if (!(y & 3)) {
616  if (y) {
617  P[0] = bytestream2_get_le16(&s->stream_ptr);
618  P[1] = bytestream2_get_le16(&s->stream_ptr);
619  }
620  flags = bytestream2_get_le16(&s->stream_ptr);
621  }
622 
623  for (x = 0; x < 4; x++, flags >>= 1)
624  *pixel_ptr++ = P[flags & 1];
625  pixel_ptr += s->stride - 4;
626  // switch to right half
627  if (y == 7) pixel_ptr -= 8 * s->stride - 4;
628  }
629 
630  } else {
631 
632  flags = bytestream2_get_le32(&s->stream_ptr);
633  P[2] = bytestream2_get_le16(&s->stream_ptr);
634  P[3] = bytestream2_get_le16(&s->stream_ptr);
635 
636  if (!(P[2] & 0x8000)) {
637 
638  /* vertical split; left & right halves are 2-color encoded */
639 
640  for (y = 0; y < 16; y++) {
641  for (x = 0; x < 4; x++, flags >>= 1)
642  *pixel_ptr++ = P[flags & 1];
643  pixel_ptr += s->stride - 4;
644  // switch to right half
645  if (y == 7) {
646  pixel_ptr -= 8 * s->stride - 4;
647  P[0] = P[2];
648  P[1] = P[3];
649  flags = bytestream2_get_le32(&s->stream_ptr);
650  }
651  }
652 
653  } else {
654 
655  /* horizontal split; top & bottom halves are 2-color encoded */
656 
657  for (y = 0; y < 8; y++) {
658  if (y == 4) {
659  P[0] = P[2];
660  P[1] = P[3];
661  flags = bytestream2_get_le32(&s->stream_ptr);
662  }
663 
664  for (x = 0; x < 8; x++, flags >>= 1)
665  *pixel_ptr++ = P[flags & 1];
666  pixel_ptr += s->line_inc;
667  }
668  }
669  }
670 
671  /* report success */
672  return 0;
673 }
674 
676 {
677  int x, y;
678  uint16_t P[4];
679  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
680 
681  /* 4-color encoding */
682  for (x = 0; x < 4; x++)
683  P[x] = bytestream2_get_le16(&s->stream_ptr);
684 
685  if (!(P[0] & 0x8000)) {
686  if (!(P[2] & 0x8000)) {
687 
688  /* 1 of 4 colors for each pixel */
689  for (y = 0; y < 8; y++) {
690  /* get the next set of 8 2-bit flags */
691  int flags = bytestream2_get_le16(&s->stream_ptr);
692  for (x = 0; x < 8; x++, flags >>= 2)
693  *pixel_ptr++ = P[flags & 0x03];
694  pixel_ptr += s->line_inc;
695  }
696 
697  } else {
698  uint32_t flags;
699 
700  /* 1 of 4 colors for each 2x2 block */
701  flags = bytestream2_get_le32(&s->stream_ptr);
702 
703  for (y = 0; y < 8; y += 2) {
704  for (x = 0; x < 8; x += 2, flags >>= 2) {
705  pixel_ptr[x ] =
706  pixel_ptr[x + 1 ] =
707  pixel_ptr[x + s->stride] =
708  pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
709  }
710  pixel_ptr += s->stride * 2;
711  }
712 
713  }
714  } else {
715  uint64_t flags;
716 
717  /* 1 of 4 colors for each 2x1 or 1x2 block */
718  flags = bytestream2_get_le64(&s->stream_ptr);
719  if (!(P[2] & 0x8000)) {
720  for (y = 0; y < 8; y++) {
721  for (x = 0; x < 8; x += 2, flags >>= 2) {
722  pixel_ptr[x ] =
723  pixel_ptr[x + 1] = P[flags & 0x03];
724  }
725  pixel_ptr += s->stride;
726  }
727  } else {
728  for (y = 0; y < 8; y += 2) {
729  for (x = 0; x < 8; x++, flags >>= 2) {
730  pixel_ptr[x ] =
731  pixel_ptr[x + s->stride] = P[flags & 0x03];
732  }
733  pixel_ptr += s->stride * 2;
734  }
735  }
736  }
737 
738  /* report success */
739  return 0;
740 }
741 
743 {
744  int x, y;
745  uint16_t P[8];
746  int flags = 0;
747  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
748 
749  for (x = 0; x < 4; x++)
750  P[x] = bytestream2_get_le16(&s->stream_ptr);
751 
752  /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
753  * either top and bottom or left and right halves */
754  if (!(P[0] & 0x8000)) {
755 
756  /* 4-color encoding for each quadrant */
757  for (y = 0; y < 16; y++) {
758  // new values for each 4x4 block
759  if (!(y & 3)) {
760  if (y)
761  for (x = 0; x < 4; x++)
762  P[x] = bytestream2_get_le16(&s->stream_ptr);
763  flags = bytestream2_get_le32(&s->stream_ptr);
764  }
765 
766  for (x = 0; x < 4; x++, flags >>= 2)
767  *pixel_ptr++ = P[flags & 0x03];
768 
769  pixel_ptr += s->stride - 4;
770  // switch to right half
771  if (y == 7) pixel_ptr -= 8 * s->stride - 4;
772  }
773 
774  } else {
775  // vertical split?
776  int vert;
777  uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
778 
779  for (x = 4; x < 8; x++)
780  P[x] = bytestream2_get_le16(&s->stream_ptr);
781  vert = !(P[4] & 0x8000);
782 
783  /* 4-color encoding for either left and right or top and bottom
784  * halves */
785 
786  for (y = 0; y < 16; y++) {
787  for (x = 0; x < 4; x++, flags >>= 2)
788  *pixel_ptr++ = P[flags & 0x03];
789 
790  if (vert) {
791  pixel_ptr += s->stride - 4;
792  // switch to right half
793  if (y == 7) pixel_ptr -= 8 * s->stride - 4;
794  } else if (y & 1) pixel_ptr += s->line_inc;
795 
796  // load values for second half
797  if (y == 7) {
798  memcpy(P, P + 4, 8);
799  flags = bytestream2_get_le64(&s->stream_ptr);
800  }
801  }
802  }
803 
804  /* report success */
805  return 0;
806 }
807 
809 {
810  int x, y;
811  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
812 
813  /* 64-color encoding (each pixel in block is a different color) */
814  for (y = 0; y < 8; y++) {
815  for (x = 0; x < 8; x++)
816  pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
817  pixel_ptr += s->stride;
818  }
819 
820  /* report success */
821  return 0;
822 }
823 
825 {
826  int x, y;
827  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
828 
829  /* 16-color block encoding: each 2x2 block is a different color */
830  for (y = 0; y < 8; y += 2) {
831  for (x = 0; x < 8; x += 2) {
832  pixel_ptr[x ] =
833  pixel_ptr[x + 1 ] =
834  pixel_ptr[x + s->stride] =
835  pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
836  }
837  pixel_ptr += s->stride * 2;
838  }
839 
840  /* report success */
841  return 0;
842 }
843 
845 {
846  int x, y;
847  uint16_t P[2];
848  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
849 
850  /* 4-color block encoding: each 4x4 block is a different color */
851  for (y = 0; y < 8; y++) {
852  if (!(y & 3)) {
853  P[0] = bytestream2_get_le16(&s->stream_ptr);
854  P[1] = bytestream2_get_le16(&s->stream_ptr);
855  }
856  for (x = 0; x < 8; x++)
857  pixel_ptr[x] = P[x >> 2];
858  pixel_ptr += s->stride;
859  }
860 
861  /* report success */
862  return 0;
863 }
864 
866 {
867  int x, y;
868  uint16_t pix;
869  uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
870 
871  /* 1-color encoding: the whole block is 1 solid color */
872  pix = bytestream2_get_le16(&s->stream_ptr);
873 
874  for (y = 0; y < 8; y++) {
875  for (x = 0; x < 8; x++)
876  pixel_ptr[x] = pix;
877  pixel_ptr += s->stride;
878  }
879 
880  /* report success */
881  return 0;
882 }
883 
893 };
894 
904 };
905 
907 {
908  int x, y;
909  unsigned char opcode;
910  int ret;
911  GetBitContext gb;
912 
913  bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
914  if (!s->is_16bpp) {
915  /* this is PAL8, so make the palette available */
916  memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
917 
918  s->stride = frame->linesize[0];
919  } else {
920  s->stride = frame->linesize[0] >> 1;
921  s->mv_ptr = s->stream_ptr;
922  bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
923  }
924  s->line_inc = s->stride - 8;
925  s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
926  + (s->avctx->width - 8) * (1 + s->is_16bpp);
927 
929  for (y = 0; y < s->avctx->height; y += 8) {
930  for (x = 0; x < s->avctx->width; x += 8) {
931  if (get_bits_left(&gb) < 4)
932  return;
933  opcode = get_bits(&gb, 4);
934 
935  ff_tlog(s->avctx,
936  " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
937  x, y, opcode, bytestream2_tell(&s->stream_ptr));
938 
939  if (!s->is_16bpp) {
940  s->pixel_ptr = frame->data[0] + x
941  + y*frame->linesize[0];
942  ret = ipvideo_decode_block[opcode](s, frame);
943  } else {
944  s->pixel_ptr = frame->data[0] + x*2
945  + y*frame->linesize[0];
946  ret = ipvideo_decode_block16[opcode](s, frame);
947  }
948  if (ret != 0) {
949  av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
950  s->avctx->frame_number, x, y);
951  return;
952  }
953  }
954  }
955  if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
957  "decode finished with %d bytes left over\n",
959  }
960 }
961 
963 {
964  IpvideoContext *s = avctx->priv_data;
965 
966  s->avctx = avctx;
967 
968  s->is_16bpp = avctx->bits_per_coded_sample == 16;
970 
971  ff_hpeldsp_init(&s->hdsp, avctx->flags);
972 
973  s->last_frame = av_frame_alloc();
975  if (!s->last_frame || !s->second_last_frame) {
978  return AVERROR(ENOMEM);
979  }
980 
981  return 0;
982 }
983 
985  void *data, int *got_frame,
986  AVPacket *avpkt)
987 {
988  const uint8_t *buf = avpkt->data;
989  int buf_size = avpkt->size;
990  IpvideoContext *s = avctx->priv_data;
991  AVFrame *frame = data;
992  int ret;
993 
997  }
998 
999  if (buf_size < 2)
1000  return AVERROR_INVALIDDATA;
1001 
1002  /* decoding map contains 4 bits of information per 8x8 block */
1003  s->decoding_map_size = AV_RL16(avpkt->data);
1004 
1005  /* compressed buffer needs to be large enough to at least hold an entire
1006  * decoding map */
1007  if (buf_size < s->decoding_map_size + 2)
1008  return buf_size;
1009 
1010 
1011  s->decoding_map = buf + 2;
1012  bytestream2_init(&s->stream_ptr, buf + 2 + s->decoding_map_size,
1013  buf_size - s->decoding_map_size);
1014 
1015  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1016  return ret;
1017 
1018  if (!s->is_16bpp) {
1019  int size;
1020  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1021  if (pal && size == AVPALETTE_SIZE) {
1022  frame->palette_has_changed = 1;
1023  memcpy(s->pal, pal, AVPALETTE_SIZE);
1024  } else if (pal) {
1025  av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1026  }
1027  }
1028 
1029  ipvideo_decode_opcodes(s, frame);
1030 
1031  *got_frame = 1;
1032 
1033  /* shuffle frames */
1036  if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1037  return ret;
1038 
1039  /* report that the buffer was completely consumed */
1040  return buf_size;
1041 }
1042 
1044 {
1045  IpvideoContext *s = avctx->priv_data;
1046 
1049 
1050  return 0;
1051 }
1052 
1054  .name = "interplayvideo",
1055  .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1056  .type = AVMEDIA_TYPE_VIDEO,
1058  .priv_data_size = sizeof(IpvideoContext),
1060  .close = ipvideo_decode_end,
1062  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,
1063 };
#define ff_tlog(ctx,...)
Definition: internal.h:65
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define P
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
const unsigned char * decoding_map
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
unsigned char * pixel_ptr
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
Definition: vf_geq.c:46
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1658
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
#define src
Definition: vp8dsp.c:254
#define sample
AVCodec.
Definition: avcodec.h:3681
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static int(*const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame)
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:388
static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3126
#define av_log(a,...)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
AVFrame * last_frame
static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1403
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: avcodec.h:1429
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
GetByteContext stream_ptr
static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
Half-pel DSP context.
Definition: hpeldsp.h:45
static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
int width
picture width / height.
Definition: avcodec.h:1919
static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
AVFrame * second_last_frame
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Half-pel DSP functions.
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
main external API structure.
Definition: avcodec.h:1732
static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
GetByteContext mv_ptr
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:953
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
HpelDSPContext hdsp
void * buf
Definition: avisynth_c.h:690
static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
AVCodecContext * avctx
static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:335
static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
AVCodec ff_interplay_video_decoder
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:498
static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
uint32_t pal[256]
int
static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition: avcodec.h:1065
int upper_motion_limit_offset
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:339
static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
void * priv_data
Definition: avcodec.h:1774
static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2525
static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2257
#define FFSWAP(type, a, b)
Definition: common.h:99
static int(*const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame)
static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
This structure stores compressed data.
Definition: avcodec.h:1634
static int ipvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1389
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:994
for(j=16;j >0;--j)