]> git.sesse.net Git - ffmpeg/blob - libavcodec/interplayvideo.c
40f7cd79228c33b6e2b17ccfdc4078f089896430
[ffmpeg] / libavcodec / interplayvideo.c
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 libavcodec/interplayvideo.c
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 #include <unistd.h>
41
42 #include "avcodec.h"
43 #include "bytestream.h"
44 #include "dsputil.h"
45
46 #define PALETTE_COUNT 256
47
48 /* debugging support */
49 #define DEBUG_INTERPLAY 0
50 #if DEBUG_INTERPLAY
51 #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
52 #else
53 static inline void debug_interplay(const char *format, ...) { }
54 #endif
55
56 typedef struct IpvideoContext {
57
58     AVCodecContext *avctx;
59     DSPContext dsp;
60     AVFrame second_last_frame;
61     AVFrame last_frame;
62     AVFrame current_frame;
63     const unsigned char *decoding_map;
64     int decoding_map_size;
65
66     const unsigned char *buf;
67     int size;
68
69     const unsigned char *stream_ptr;
70     const unsigned char *stream_end;
71     unsigned char *pixel_ptr;
72     int line_inc;
73     int stride;
74     int upper_motion_limit_offset;
75
76 } IpvideoContext;
77
78 #define CHECK_STREAM_PTR(n) \
79   if (s->stream_end - s->stream_ptr < n) { \
80     av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
81       s->stream_ptr + n, s->stream_end); \
82     return -1; \
83   }
84
85 static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
86 {
87     int current_offset = s->pixel_ptr - s->current_frame.data[0];
88     int motion_offset = current_offset + delta_y * s->stride + delta_x;
89     if (motion_offset < 0) {
90         av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
91         return -1;
92     } else if (motion_offset > s->upper_motion_limit_offset) {
93         av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
94             motion_offset, s->upper_motion_limit_offset);
95         return -1;
96     }
97     s->dsp.put_pixels_tab[1][0](s->pixel_ptr, src->data[0] + motion_offset, s->stride, 8);
98     return 0;
99 }
100
101 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
102 {
103     return copy_from(s, &s->last_frame, 0, 0);
104 }
105
106 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
107 {
108     return copy_from(s, &s->second_last_frame, 0, 0);
109 }
110
111 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
112 {
113     unsigned char B;
114     int x, y;
115
116     /* copy block from 2 frames ago using a motion vector; need 1 more byte */
117     CHECK_STREAM_PTR(1);
118     B = *s->stream_ptr++;
119
120     if (B < 56) {
121         x = 8 + (B % 7);
122         y = B / 7;
123     } else {
124         x = -14 + ((B - 56) % 29);
125         y =   8 + ((B - 56) / 29);
126     }
127
128     debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
129     return copy_from(s, &s->second_last_frame, x, y);
130 }
131
132 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
133 {
134     unsigned char B;
135     int x, y;
136
137     /* copy 8x8 block from current frame from an up/left block */
138
139     /* need 1 more byte for motion */
140     CHECK_STREAM_PTR(1);
141     B = *s->stream_ptr++;
142
143     if (B < 56) {
144         x = -(8 + (B % 7));
145         y = -(B / 7);
146     } else {
147         x = -(-14 + ((B - 56) % 29));
148         y = -(  8 + ((B - 56) / 29));
149     }
150
151     debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
152     return copy_from(s, &s->current_frame, x, y);
153 }
154
155 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
156 {
157     int x, y;
158     unsigned char B, BL, BH;
159
160     /* copy a block from the previous frame; need 1 more byte */
161     CHECK_STREAM_PTR(1);
162
163     B = *s->stream_ptr++;
164     BL = B & 0x0F;
165     BH = (B >> 4) & 0x0F;
166     x = -8 + BL;
167     y = -8 + BH;
168
169     debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
170     return copy_from(s, &s->last_frame, x, y);
171 }
172
173 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
174 {
175     signed char x, y;
176
177     /* copy a block from the previous frame using an expanded range;
178      * need 2 more bytes */
179     CHECK_STREAM_PTR(2);
180
181     x = *s->stream_ptr++;
182     y = *s->stream_ptr++;
183
184     debug_interplay ("    motion bytes = %d, %d\n", x, y);
185     return copy_from(s, &s->last_frame, x, y);
186 }
187
188 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
189 {
190     /* mystery opcode? skip multiple blocks? */
191     av_log(s->avctx, AV_LOG_ERROR, "  Interplay video: Help! Mystery opcode 0x6 seen\n");
192
193     /* report success */
194     return 0;
195 }
196
197 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
198 {
199     int x, y;
200     unsigned char P[2];
201     unsigned int flags;
202
203     /* 2-color encoding */
204     CHECK_STREAM_PTR(2);
205
206     P[0] = *s->stream_ptr++;
207     P[1] = *s->stream_ptr++;
208
209     if (P[0] <= P[1]) {
210
211         /* need 8 more bytes from the stream */
212         CHECK_STREAM_PTR(8);
213
214         for (y = 0; y < 8; y++) {
215             flags = *s->stream_ptr++ | 0x100;
216             for (; flags != 1; flags >>= 1) {
217                 *s->pixel_ptr++ = P[flags & 1];
218             }
219             s->pixel_ptr += s->line_inc;
220         }
221
222     } else {
223
224         /* need 2 more bytes from the stream */
225         CHECK_STREAM_PTR(2);
226
227         flags = bytestream_get_le16(&s->stream_ptr);
228         for (y = 0; y < 8; y += 2) {
229             for (x = 0; x < 8; x += 2, flags >>= 1) {
230                 s->pixel_ptr[x                ] =
231                 s->pixel_ptr[x + 1            ] =
232                 s->pixel_ptr[x +     s->stride] =
233                 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
234             }
235             s->pixel_ptr += s->stride * 2;
236         }
237     }
238
239     /* report success */
240     return 0;
241 }
242
243 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
244 {
245     int x, y;
246     unsigned char P[2];
247     unsigned int flags = 0;
248
249     /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
250      * either top and bottom or left and right halves */
251     CHECK_STREAM_PTR(2);
252
253     P[0] = *s->stream_ptr++;
254     P[1] = *s->stream_ptr++;
255
256     if (P[0] <= P[1]) {
257
258         CHECK_STREAM_PTR(14);
259         s->stream_ptr -= 2;
260
261         for (y = 0; y < 16; y++) {
262             // new values for each 4x4 block
263             if (!(y & 3)) {
264                 P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
265                 flags = bytestream_get_le16(&s->stream_ptr);
266             }
267
268             for (x = 0; x < 4; x++, flags >>= 1) {
269                 *s->pixel_ptr++ = P[flags & 1];
270             }
271             s->pixel_ptr += s->stride - 4;
272             // switch to right half
273             if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
274         }
275
276     } else {
277
278         /* need 10 more bytes */
279         CHECK_STREAM_PTR(10);
280
281         if (s->stream_ptr[4] <= s->stream_ptr[5]) {
282
283             flags = bytestream_get_le32(&s->stream_ptr);
284
285             /* vertical split; left & right halves are 2-color encoded */
286
287             for (y = 0; y < 16; y++) {
288                 for (x = 0; x < 4; x++, flags >>= 1) {
289                     *s->pixel_ptr++ = P[flags & 1];
290                 }
291                 s->pixel_ptr += s->stride - 4;
292                 // switch to right half
293                 if (y == 7) {
294                     s->pixel_ptr -= 8 * s->stride - 4;
295                     P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
296                     flags = bytestream_get_le32(&s->stream_ptr);
297                 }
298             }
299
300         } else {
301
302             /* horizontal split; top & bottom halves are 2-color encoded */
303
304             for (y = 0; y < 8; y++) {
305                 if (y == 4) {
306                     P[0] = *s->stream_ptr++;
307                     P[1] = *s->stream_ptr++;
308                 }
309                 flags = *s->stream_ptr++ | 0x100;
310
311                 for (; flags != 1; flags >>= 1) {
312
313                     *s->pixel_ptr++ = P[flags & 1];
314                 }
315                 s->pixel_ptr += s->line_inc;
316             }
317         }
318     }
319
320     /* report success */
321     return 0;
322 }
323
324 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
325 {
326     int x, y;
327     unsigned char P[4];
328
329     /* 4-color encoding */
330     CHECK_STREAM_PTR(4);
331
332     memcpy(P, s->stream_ptr, 4);
333     s->stream_ptr += 4;
334
335     if (P[0] <= P[1]) {
336         if (P[2] <= P[3]) {
337
338             /* 1 of 4 colors for each pixel, need 16 more bytes */
339             CHECK_STREAM_PTR(16);
340
341             for (y = 0; y < 8; y++) {
342                 /* get the next set of 8 2-bit flags */
343                 int flags = bytestream_get_le16(&s->stream_ptr);
344                 for (x = 0; x < 8; x++, flags >>= 2) {
345                     *s->pixel_ptr++ = P[flags & 0x03];
346                 }
347                 s->pixel_ptr += s->line_inc;
348             }
349
350         } else {
351             uint32_t flags;
352
353             /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
354             CHECK_STREAM_PTR(4);
355
356             flags = bytestream_get_le32(&s->stream_ptr);
357
358             for (y = 0; y < 8; y += 2) {
359                 for (x = 0; x < 8; x += 2, flags >>= 2) {
360                     s->pixel_ptr[x                ] =
361                     s->pixel_ptr[x + 1            ] =
362                     s->pixel_ptr[x +     s->stride] =
363                     s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
364                 }
365                 s->pixel_ptr += s->stride * 2;
366             }
367
368         }
369     } else {
370         uint64_t flags;
371
372         /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
373         CHECK_STREAM_PTR(8);
374
375         flags = bytestream_get_le64(&s->stream_ptr);
376         if (P[2] <= P[3]) {
377             for (y = 0; y < 8; y++) {
378                 for (x = 0; x < 8; x += 2, flags >>= 2) {
379                     s->pixel_ptr[x    ] =
380                     s->pixel_ptr[x + 1] = P[flags & 0x03];
381                 }
382                 s->pixel_ptr += s->stride;
383             }
384         } else {
385             for (y = 0; y < 8; y += 2) {
386                 for (x = 0; x < 8; x++, flags >>= 2) {
387                     s->pixel_ptr[x            ] =
388                     s->pixel_ptr[x + s->stride] = P[flags & 0x03];
389                 }
390                 s->pixel_ptr += s->stride * 2;
391             }
392         }
393     }
394
395     /* report success */
396     return 0;
397 }
398
399 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
400 {
401     int x, y;
402     unsigned char P[8];
403     unsigned char B[16];
404     int flags = 0;
405     int split;
406
407     /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
408      * either top and bottom or left and right halves */
409     CHECK_STREAM_PTR(4);
410
411     memcpy(P, s->stream_ptr, 4);
412     s->stream_ptr += 4;
413
414     if (P[0] <= P[1]) {
415
416         /* 4-color encoding for each quadrant; need 28 more bytes */
417         CHECK_STREAM_PTR(28);
418         s->stream_ptr -= 4;
419
420         for (y = 0; y < 16; y++) {
421             // new values for each 4x4 block
422             if (!(y & 3)) {
423                 memcpy(P, s->stream_ptr, 4);
424                 s->stream_ptr += 4;
425                 flags = bytestream_get_le32(&s->stream_ptr);
426             }
427
428             for (x = 0; x < 4; x++, flags >>= 2) {
429                 *s->pixel_ptr++ = P[flags & 0x03];
430             }
431
432             s->pixel_ptr += s->stride - 4;
433             // switch to right half
434             if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
435         }
436
437     } else {
438
439         /* 4-color encoding for either left and right or top and bottom
440          * halves; need 20 more bytes */
441         CHECK_STREAM_PTR(20);
442
443         memcpy(B, s->stream_ptr, 8);
444         s->stream_ptr += 8;
445         memcpy(P + 4, s->stream_ptr, 4);
446         s->stream_ptr += 4;
447         memcpy(B + 8, s->stream_ptr, 8);
448         s->stream_ptr += 8;
449
450         if (P[4] <= P[5]) {
451
452             /* block is divided into left and right halves */
453             for (y = 0; y < 8; y++) {
454
455                 flags = (B[y + 8] << 8) | B[y];
456                 split = 0;
457
458                 for (x = 0; x < 8; x++, flags >>= 2) {
459                     if (x == 4)
460                         split = 4;
461                     *s->pixel_ptr++ = P[split + (flags & 0x03)];
462                 }
463
464                 s->pixel_ptr += s->line_inc;
465             }
466
467         } else {
468
469             /* block is divided into top and bottom halves */
470             split = 0;
471             for (y = 0; y < 8; y++) {
472
473                 flags = (B[y * 2 + 1] << 8) | B[y * 2];
474                 if (y == 4)
475                     split = 4;
476
477                 for (x = 0; x < 8; x++, flags >>= 2)
478                     *s->pixel_ptr++ = P[split + (flags & 0x03)];
479
480                 s->pixel_ptr += s->line_inc;
481             }
482         }
483     }
484
485     /* report success */
486     return 0;
487 }
488
489 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
490 {
491     int y;
492
493     /* 64-color encoding (each pixel in block is a different color) */
494     CHECK_STREAM_PTR(64);
495
496     for (y = 0; y < 8; y++) {
497         memcpy(s->pixel_ptr, s->stream_ptr, 8);
498         s->stream_ptr += 8;
499         s->pixel_ptr  += s->stride;
500     }
501
502     /* report success */
503     return 0;
504 }
505
506 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
507 {
508     int x, y;
509
510     /* 16-color block encoding: each 2x2 block is a different color */
511     CHECK_STREAM_PTR(16);
512
513     for (y = 0; y < 8; y += 2) {
514         for (x = 0; x < 8; x += 2) {
515             s->pixel_ptr[x                ] =
516             s->pixel_ptr[x + 1            ] =
517             s->pixel_ptr[x +     s->stride] =
518             s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
519         }
520         s->pixel_ptr += s->stride * 2;
521     }
522
523     /* report success */
524     return 0;
525 }
526
527 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
528 {
529     int y;
530     unsigned char P[4];
531     unsigned char index = 0;
532
533     /* 4-color block encoding: each 4x4 block is a different color */
534     CHECK_STREAM_PTR(4);
535
536     memcpy(P, s->stream_ptr, 4);
537     s->stream_ptr += 4;
538
539     for (y = 0; y < 8; y++) {
540         if (y < 4)
541             index = 0;
542         else
543             index = 2;
544
545         memset(s->pixel_ptr    , P[index    ], 4);
546         memset(s->pixel_ptr + 4, P[index + 1], 4);
547         s->pixel_ptr += s->stride;
548     }
549
550     /* report success */
551     return 0;
552 }
553
554 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
555 {
556     int y;
557     unsigned char pix;
558
559     /* 1-color encoding: the whole block is 1 solid color */
560     CHECK_STREAM_PTR(1);
561     pix = *s->stream_ptr++;
562
563     for (y = 0; y < 8; y++) {
564         memset(s->pixel_ptr, pix, 8);
565         s->pixel_ptr += s->stride;
566     }
567
568     /* report success */
569     return 0;
570 }
571
572 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
573 {
574     int x, y;
575     unsigned char sample[2];
576
577     /* dithered encoding */
578     CHECK_STREAM_PTR(2);
579     sample[0] = *s->stream_ptr++;
580     sample[1] = *s->stream_ptr++;
581
582     for (y = 0; y < 8; y++) {
583         for (x = 0; x < 8; x += 2) {
584             *s->pixel_ptr++ = sample[  y & 1 ];
585             *s->pixel_ptr++ = sample[!(y & 1)];
586         }
587         s->pixel_ptr += s->line_inc;
588     }
589
590     /* report success */
591     return 0;
592 }
593
594 static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
595     ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
596     ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
597     ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
598     ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
599     ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
600     ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
601     ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
602     ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
603 };
604
605 static void ipvideo_decode_opcodes(IpvideoContext *s)
606 {
607     int x, y;
608     int index = 0;
609     unsigned char opcode;
610     int ret;
611     int code_counts[16] = {0};
612     static int frame = 0;
613
614     debug_interplay("------------------ frame %d\n", frame);
615     frame++;
616
617     /* this is PAL8, so make the palette available */
618     memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
619
620     s->stride = s->current_frame.linesize[0];
621     s->stream_ptr = s->buf + 14;  /* data starts 14 bytes in */
622     s->stream_end = s->buf + s->size;
623     s->line_inc = s->stride - 8;
624     s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
625         + s->avctx->width - 8;
626
627     for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
628         for (x = y; x < y + s->avctx->width; x += 8) {
629             /* bottom nibble first, then top nibble (which makes it
630              * hard to use a GetBitcontext) */
631             if (index & 1)
632                 opcode = s->decoding_map[index >> 1] >> 4;
633             else
634                 opcode = s->decoding_map[index >> 1] & 0xF;
635             index++;
636
637             debug_interplay("  block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
638                 x - y, y / s->stride, opcode, s->stream_ptr);
639             code_counts[opcode]++;
640
641             s->pixel_ptr = s->current_frame.data[0] + x;
642             ret = ipvideo_decode_block[opcode](s);
643             if (ret != 0) {
644                 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
645                     frame, x - y, y / s->stride);
646                 return;
647             }
648         }
649     }
650     if (s->stream_end - s->stream_ptr > 1) {
651         av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
652             s->stream_end - s->stream_ptr);
653     }
654 }
655
656 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
657 {
658     IpvideoContext *s = avctx->priv_data;
659
660     s->avctx = avctx;
661
662     if (s->avctx->palctrl == NULL) {
663         av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
664         return -1;
665     }
666
667     avctx->pix_fmt = PIX_FMT_PAL8;
668     dsputil_init(&s->dsp, avctx);
669
670     /* decoding map contains 4 bits of information per 8x8 block */
671     s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
672
673     s->current_frame.data[0] = s->last_frame.data[0] =
674     s->second_last_frame.data[0] = NULL;
675
676     return 0;
677 }
678
679 static int ipvideo_decode_frame(AVCodecContext *avctx,
680                                 void *data, int *data_size,
681                                 const uint8_t *buf, int buf_size)
682 {
683     IpvideoContext *s = avctx->priv_data;
684     AVPaletteControl *palette_control = avctx->palctrl;
685
686     /* compressed buffer needs to be large enough to at least hold an entire
687      * decoding map */
688     if (buf_size < s->decoding_map_size)
689         return buf_size;
690
691     s->decoding_map = buf;
692     s->buf = buf + s->decoding_map_size;
693     s->size = buf_size - s->decoding_map_size;
694
695     s->current_frame.reference = 3;
696     if (avctx->get_buffer(avctx, &s->current_frame)) {
697         av_log(avctx, AV_LOG_ERROR, "  Interplay Video: get_buffer() failed\n");
698         return -1;
699     }
700
701     ipvideo_decode_opcodes(s);
702
703     if (palette_control->palette_changed) {
704         palette_control->palette_changed = 0;
705         s->current_frame.palette_has_changed = 1;
706     }
707
708     *data_size = sizeof(AVFrame);
709     *(AVFrame*)data = s->current_frame;
710
711     /* shuffle frames */
712     if (s->second_last_frame.data[0])
713         avctx->release_buffer(avctx, &s->second_last_frame);
714     s->second_last_frame = s->last_frame;
715     s->last_frame = s->current_frame;
716     s->current_frame.data[0] = NULL;  /* catch any access attempts */
717
718     /* report that the buffer was completely consumed */
719     return buf_size;
720 }
721
722 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
723 {
724     IpvideoContext *s = avctx->priv_data;
725
726     /* release the last frame */
727     if (s->last_frame.data[0])
728         avctx->release_buffer(avctx, &s->last_frame);
729     if (s->second_last_frame.data[0])
730         avctx->release_buffer(avctx, &s->second_last_frame);
731
732     return 0;
733 }
734
735 AVCodec interplay_video_decoder = {
736     "interplayvideo",
737     CODEC_TYPE_VIDEO,
738     CODEC_ID_INTERPLAY_VIDEO,
739     sizeof(IpvideoContext),
740     ipvideo_decode_init,
741     NULL,
742     ipvideo_decode_end,
743     ipvideo_decode_frame,
744     CODEC_CAP_DR1,
745     .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
746 };