Revision 293fe6da libavcodec/flashsv.c

View differences:

libavcodec/flashsv.c
49 49

  
50 50
#include <stdio.h>
51 51
#include <stdlib.h>
52
#include <zlib.h>
52 53

  
53 54
#include "avcodec.h"
54 55
#include "get_bits.h"
55 56

  
56
#include <zlib.h>
57

  
58 57
typedef struct FlashSVContext {
59 58
    AVCodecContext *avctx;
60
    AVFrame frame;
61
    int image_width, image_height;
62
    int block_width, block_height;
63
    uint8_t* tmpblock;
64
    int block_size;
65
    z_stream zstream;
59
    AVFrame         frame;
60
    int             image_width, image_height;
61
    int             block_width, block_height;
62
    uint8_t        *tmpblock;
63
    int             block_size;
64
    z_stream        zstream;
66 65
} FlashSVContext;
67 66

  
68 67

  
69 68
static void copy_region(uint8_t *sptr, uint8_t *dptr,
70
        int dx, int dy, int h, int w, int stride)
69
                        int dx, int dy, int h, int w, int stride)
71 70
{
72 71
    int i;
73 72

  
74
    for (i = dx+h; i > dx; i--)
75
    {
76
        memcpy(dptr+(i*stride)+dy*3, sptr, w*3);
77
        sptr += w*3;
73
    for (i = dx + h; i > dx; i--) {
74
        memcpy(dptr + (i * stride) + dy * 3, sptr, w * 3);
75
        sptr += w * 3;
78 76
    }
79 77
}
80 78

  
......
84 82
    FlashSVContext *s = avctx->priv_data;
85 83
    int zret; // Zlib return code
86 84

  
87
    s->avctx = avctx;
85
    s->avctx          = avctx;
88 86
    s->zstream.zalloc = Z_NULL;
89
    s->zstream.zfree = Z_NULL;
87
    s->zstream.zfree  = Z_NULL;
90 88
    s->zstream.opaque = Z_NULL;
91 89
    zret = inflateInit(&(s->zstream));
92 90
    if (zret != Z_OK) {
......
100 98
}
101 99

  
102 100

  
103
static int flashsv_decode_frame(AVCodecContext *avctx,
104
                                    void *data, int *data_size,
105
                                    AVPacket *avpkt)
101
static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
102
                                int *data_size, AVPacket *avpkt)
106 103
{
107 104
    const uint8_t *buf = avpkt->data;
108
    int buf_size = avpkt->size;
109
    FlashSVContext *s = avctx->priv_data;
105
    int buf_size       = avpkt->size;
106
    FlashSVContext *s  = avctx->priv_data;
110 107
    int h_blocks, v_blocks, h_part, v_part, i, j;
111 108
    GetBitContext gb;
112 109

  
......
119 116
    init_get_bits(&gb, buf, buf_size * 8);
120 117

  
121 118
    /* start to parse the bitstream */
122
    s->block_width = 16* (get_bits(&gb, 4)+1);
123
    s->image_width =     get_bits(&gb,12);
124
    s->block_height= 16* (get_bits(&gb, 4)+1);
125
    s->image_height=     get_bits(&gb,12);
119
    s->block_width  = 16 * (get_bits(&gb,  4) + 1);
120
    s->image_width  =       get_bits(&gb, 12);
121
    s->block_height = 16 * (get_bits(&gb,  4) + 1);
122
    s->image_height =       get_bits(&gb, 12);
126 123

  
127 124
    /* calculate amount of blocks and the size of the border blocks */
128
    h_blocks = s->image_width / s->block_width;
129
    h_part = s->image_width % s->block_width;
125
    h_blocks = s->image_width  / s->block_width;
126
    h_part   = s->image_width  % s->block_width;
130 127
    v_blocks = s->image_height / s->block_height;
131
    v_part = s->image_height % s->block_height;
128
    v_part   = s->image_height % s->block_height;
132 129

  
133 130
    /* the block size could change between frames, make sure the buffer
134 131
     * is large enough, if not, get a larger one */
135
    if(s->block_size < s->block_width*s->block_height) {
132
    if (s->block_size < s->block_width * s->block_height) {
136 133
        av_free(s->tmpblock);
137
        if ((s->tmpblock = av_malloc(3*s->block_width*s->block_height)) == NULL) {
134
        if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
138 135
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
139 136
            return -1;
140 137
        }
141 138
    }
142
    s->block_size = s->block_width*s->block_height;
139
    s->block_size = s->block_width * s->block_height;
143 140

  
144 141
    /* init the image size once */
145
    if((avctx->width==0) && (avctx->height==0)){
146
        avctx->width = s->image_width;
142
    if ((avctx->width == 0) && (avctx->height == 0)) {
143
        avctx->width  = s->image_width;
147 144
        avctx->height = s->image_height;
148 145
    }
149 146

  
150 147
    /* check for changes of image width and image height */
151 148
    if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
152 149
        av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
153
        av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\n",avctx->height,
154
        avctx->width,s->image_height,s->image_width);
150
        av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\n", avctx->height,
151
        avctx->width,s->image_height, s->image_width);
155 152
        return -1;
156 153
    }
157 154

  
158 155
    av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
159
        s->image_width, s->image_height, s->block_width, s->block_height,
160
        h_blocks, v_blocks, h_part, v_part);
156
           s->image_width, s->image_height, s->block_width, s->block_height,
157
           h_blocks, v_blocks, h_part, v_part);
161 158

  
162
    s->frame.reference = 1;
159
    s->frame.reference    = 1;
163 160
    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
164
    if(avctx->reget_buffer(avctx, &s->frame) < 0){
165
      av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
166
      return -1;
161
    if (avctx->reget_buffer(avctx, &s->frame) < 0) {
162
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
163
        return -1;
167 164
    }
168 165

  
169 166
    /* loop over all block columns */
170
    for (j = 0; j < v_blocks + (v_part?1:0); j++)
171
    {
167
    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
172 168

  
173
        int hp = j*s->block_height; // horiz position in frame
174
        int hs = (j<v_blocks)?s->block_height:v_part; // size of block
169
        int hp = j * s->block_height; // horiz position in frame
170
        int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
175 171

  
176 172

  
177 173
        /* loop over all block rows */
178
        for (i = 0; i < h_blocks + (h_part?1:0); i++)
179
        {
180
            int wp = i*s->block_width; // vert position in frame
181
            int ws = (i<h_blocks)?s->block_width:h_part; // size of block
174
        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
175
            int wp = i * s->block_width; // vert position in frame
176
            int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
182 177

  
183 178
            /* get the size of the compressed zlib chunk */
184 179
            int size = get_bits(&gb, 16);
......
193 188
            } else {
194 189
                /* decompress block */
195 190
                int ret = inflateReset(&(s->zstream));
196
                if (ret != Z_OK)
197
                {
191
                if (ret != Z_OK) {
198 192
                    av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
199 193
                    /* return -1; */
200 194
                }
201
                s->zstream.next_in = buf+(get_bits_count(&gb)/8);
202
                s->zstream.avail_in = size;
203
                s->zstream.next_out = s->tmpblock;
195
                s->zstream.next_in   = buf + (get_bits_count(&gb) / 8);
196
                s->zstream.avail_in  = size;
197
                s->zstream.next_out  = s->tmpblock;
204 198
                s->zstream.avail_out = s->block_size*3;
205 199
                ret = inflate(&(s->zstream), Z_FINISH);
206
                if (ret == Z_DATA_ERROR)
207
                {
200
                if (ret == Z_DATA_ERROR) {
208 201
                    av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
209 202
                    inflateSync(&(s->zstream));
210 203
                    ret = inflate(&(s->zstream), Z_FINISH);
211 204
                }
212 205

  
213
                if ((ret != Z_OK) && (ret != Z_STREAM_END))
214
                {
206
                if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
215 207
                    av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
216 208
                    /* return -1; */
217 209
                }
218
                copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]);
219
                skip_bits_long(&gb, 8*size);   /* skip the consumed bits */
210
                copy_region(s->tmpblock, s->frame.data[0], s->image_height - (hp + hs + 1),
211
                            wp, hs, ws, s->frame.linesize[0]);
212
                skip_bits_long(&gb, 8 * size);   /* skip the consumed bits */
220 213
            }
221 214
        }
222 215
    }
......
224 217
    *data_size = sizeof(AVFrame);
225 218
    *(AVFrame*)data = s->frame;
226 219

  
227
    if ((get_bits_count(&gb)/8) != buf_size)
220
    if ((get_bits_count(&gb) / 8) != buf_size)
228 221
        av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
229
            buf_size, (get_bits_count(&gb)/8));
222
               buf_size, (get_bits_count(&gb) / 8));
230 223

  
231 224
    /* report that the buffer was completely consumed */
232 225
    return buf_size;

Also available in: Unified diff