annotate msrledec.c @ 8266:ce2bb0ae8587 libavcodec

Make decoding run of pixels in MS RLE saner. This is based on patches by Alex Converse and Mans Rullgard Thread "[PATCH] Fix MSRLE type punning"
author kostya
date Sat, 06 Dec 2008 09:05:30 +0000
parents bb310197d59f
children 2acf0ae7b041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
1 /*
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
2 * Micrsoft RLE Decoder
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
3 * Copyright (C) 2008 Konstantin Shishkov
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
15 * Lesser General Public License for more details.
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
16 *
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
20 */
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
21
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
22 /**
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
23 * @file msrledec.c
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
24 * MS RLE Decoder based on decoder by Mike Melanson and my own for TSCC
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
25 * For more information about the MS RLE format, visit:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
26 * http://www.multimedia.cx/msrle.txt
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
27 */
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
28
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
29 #include "avcodec.h"
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
30
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
31 #define FETCH_NEXT_STREAM_BYTE() \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
32 if (stream_ptr >= data_size) \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
33 { \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
34 av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
35 return -1; \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
36 } \
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
37 stream_byte = data[stream_ptr++];
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
38
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
39 static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
40 const uint8_t *data, int data_size)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
41 {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
42 int stream_ptr = 0;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
43 unsigned char rle_code;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
44 unsigned char extra_byte, odd_pixel;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
45 unsigned char stream_byte;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
46 int pixel_ptr = 0;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
47 int row_dec = pic->linesize[0];
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
48 int row_ptr = (avctx->height - 1) * row_dec;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
49 int frame_size = row_dec * avctx->height;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
50 int i;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
51
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
52 while (row_ptr >= 0) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
53 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
54 rle_code = stream_byte;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
55 if (rle_code == 0) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
56 /* fetch the next byte to see how to handle escape code */
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
57 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
58 if (stream_byte == 0) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
59 /* line is done, goto the next one */
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
60 row_ptr -= row_dec;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
61 pixel_ptr = 0;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
62 } else if (stream_byte == 1) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
63 /* decode is done */
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
64 return 0;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
65 } else if (stream_byte == 2) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
66 /* reposition frame decode coordinates */
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
67 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
68 pixel_ptr += stream_byte;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
69 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
70 row_ptr -= stream_byte * row_dec;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
71 } else {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
72 // copy pixels from encoded stream
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
73 odd_pixel = stream_byte & 1;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
74 rle_code = (stream_byte + 1) / 2;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
75 extra_byte = rle_code & 0x01;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
76 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
77 (row_ptr < 0)) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
78 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
79 return -1;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
80 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
81
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
82 for (i = 0; i < rle_code; i++) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
83 if (pixel_ptr >= avctx->width)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
84 break;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
85 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
86 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
87 pixel_ptr++;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
88 if (i + 1 == rle_code && odd_pixel)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
89 break;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
90 if (pixel_ptr >= avctx->width)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
91 break;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
92 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
93 pixel_ptr++;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
94 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
95
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
96 // if the RLE code is odd, skip a byte in the stream
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
97 if (extra_byte)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
98 stream_ptr++;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
99 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
100 } else {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
101 // decode a run of data
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
102 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
103 (row_ptr < 0)) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
104 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
105 return -1;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
106 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
107 FETCH_NEXT_STREAM_BYTE();
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
108 for (i = 0; i < rle_code; i++) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
109 if (pixel_ptr >= avctx->width)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
110 break;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
111 if ((i & 1) == 0)
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
112 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
113 else
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
114 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
115 pixel_ptr++;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
116 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
117 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
118 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
119
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
120 /* one last sanity check on the way out */
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
121 if (stream_ptr < data_size) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
122 av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
123 stream_ptr, data_size);
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
124 return -1;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
125 }
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
126
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
127 return 0;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
128 }
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
129
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
130
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
131 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
132 const uint8_t *data, int srcsize)
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
133 {
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
134 uint8_t *output, *output_end;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
135 const uint8_t* src = data;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
136 int p1, p2, line=avctx->height, pos=0, i;
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
137 uint16_t pix16;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
138 uint32_t pix32;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2510
diff changeset
139
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
140 output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
141 output_end = pic->data[0] + (avctx->height) * pic->linesize[0];
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
142 while(src < data + srcsize) {
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
143 p1 = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
144 if(p1 == 0) { //Escape code
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
145 p2 = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
146 if(p2 == 0) { //End-of-line
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
147 output = pic->data[0] + (--line) * pic->linesize[0];
8265
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
148 if (line < 0){
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
149 av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
2455
d74d342cabb9 Check pointers before writing to memory
rtognimp
parents: 2453
diff changeset
150 return -1;
8265
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
151 }
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
152 pos = 0;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
153 continue;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
154 } else if(p2 == 1) { //End-of-picture
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
155 return 0;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
156 } else if(p2 == 2) { //Skip
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
157 p1 = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
158 p2 = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
159 line -= p2;
8265
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
160 if (line < 0){
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
161 av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
2455
d74d342cabb9 Check pointers before writing to memory
rtognimp
parents: 2453
diff changeset
162 return -1;
8265
bb310197d59f Print out some errors found during MS RLE decoding
kostya
parents: 7885
diff changeset
163 }
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
164 pos += p1;
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
165 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
166 continue;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
167 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
168 // Copy data
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
169 if (output + p2 * (depth >> 3) > output_end) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
170 src += p2 * (depth >> 3);
2455
d74d342cabb9 Check pointers before writing to memory
rtognimp
parents: 2453
diff changeset
171 continue;
d74d342cabb9 Check pointers before writing to memory
rtognimp
parents: 2453
diff changeset
172 }
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
173 if ((depth == 8) || (depth == 24)) {
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
174 for(i = 0; i < p2 * (depth >> 3); i++) {
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
175 *output++ = *src++;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
176 }
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
177 // RLE8 copy is actually padded - and runs are not!
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
178 if(depth == 8 && (p2 & 1)) {
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
179 src++;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
180 }
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
181 } else if (depth == 16) {
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
182 for(i = 0; i < p2; i++) {
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 3947
diff changeset
183 pix16 = AV_RL16(src);
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
184 src += 2;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
185 *(uint16_t*)output = pix16;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
186 output += 2;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
187 }
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
188 } else if (depth == 32) {
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
189 for(i = 0; i < p2; i++) {
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 3947
diff changeset
190 pix32 = AV_RL32(src);
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
191 src += 4;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
192 *(uint32_t*)output = pix32;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
193 output += 4;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
194 }
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
195 }
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
196 pos += p2;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
197 } else { //Run of pixels
8266
ce2bb0ae8587 Make decoding run of pixels in MS RLE saner.
kostya
parents: 8265
diff changeset
198 uint8_t pix[3]; //original pixel
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
199 switch(depth){
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
200 case 8: pix[0] = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
201 break;
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 3947
diff changeset
202 case 16: pix16 = AV_RL16(src);
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
203 src += 2;
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
204 break;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
205 case 24: pix[0] = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
206 pix[1] = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
207 pix[2] = *src++;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
208 break;
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 3947
diff changeset
209 case 32: pix32 = AV_RL32(src);
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
210 src += 4;
2481
0803adcb3ec3 Add 32bit RGB support
rtognimp
parents: 2455
diff changeset
211 break;
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
212 }
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
213 if (output + p1 * (depth >> 3) > output_end)
2455
d74d342cabb9 Check pointers before writing to memory
rtognimp
parents: 2453
diff changeset
214 continue;
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
215 for(i = 0; i < p1; i++) {
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
216 switch(depth){
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
217 case 8: *output++ = pix[0];
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
218 break;
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
219 case 16: *(uint16_t*)output = pix16;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
220 output += 2;
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
221 break;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
222 case 24: *output++ = pix[0];
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
223 *output++ = pix[1];
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
224 *output++ = pix[2];
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
225 break;
3121
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
226 case 32: *(uint32_t*)output = pix32;
99cbff5f8038 make TSCC endian-safe, PPC testing courtesy of Diego B.
melanson
parents: 3036
diff changeset
227 output += 4;
2481
0803adcb3ec3 Add 32bit RGB support
rtognimp
parents: 2455
diff changeset
228 break;
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
229 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
230 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
231 pos += p1;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
232 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
233 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2510
diff changeset
234
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
235 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no End-of-picture code\n");
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
236 return 0;
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
237 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
238
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
239
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
240 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
241 const uint8_t* data, int data_size)
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
242 {
7885
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
243 switch(depth){
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
244 case 4:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
245 return msrle_decode_pal4(avctx, pic, data, data_size);
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
246 case 8:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
247 case 16:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
248 case 24:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
249 case 32:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
250 return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
251 default:
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
252 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
253 return -1;
f874e1d5cf07 Factorize out code used for MS RLE format decoding in different decoders.
kostya
parents: 7823
diff changeset
254 }
2170
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
255 }
51da590b31a3 TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
256