Browse code

Add meaningful error codes and constants.

Replace literals with named constants in several pieces of code
like 'return -1' and 'case 0xab'.
Change the way decoder handles absence of image data in a file:
notify gif_decode_frame() caller with got_picture set to zero
instead of returning -1.

Signed-off-by: Vitaliy E Sugrobov <vsugrob@hotmail.com>

Vitaliy E Sugrobov authored on 2012/11/30 17:58:52
Showing 1 changed files
... ...
@@ -31,6 +31,10 @@
31 31
 #define GCE_DISPOSAL_INPLACE    1
32 32
 #define GCE_DISPOSAL_BACKGROUND 2
33 33
 #define GCE_DISPOSAL_RESTORE    3
34
+#define GIF_TRAILER                 0x3b
35
+#define GIF_EXTENSION_INTRODUCER    0x21
36
+#define GIF_IMAGE_SEPARATOR         0x2c
37
+#define GIF_GCE_EXT_LABEL           0xf9
34 38
 
35 39
 typedef struct GifState {
36 40
     AVFrame picture;
... ...
@@ -170,7 +174,7 @@ static int gif_read_extension(GifState *s)
170 170
     av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
171 171
 
172 172
     switch(ext_code) {
173
-    case 0xf9:
173
+    case GIF_GCE_EXT_LABEL:
174 174
         if (ext_len != 4)
175 175
             goto discard_ext;
176 176
         s->transparent_color_index = -1;
... ...
@@ -248,28 +252,32 @@ static int gif_read_header1(GifState *s)
248 248
     return 0;
249 249
 }
250 250
 
251
-static int gif_parse_next_image(GifState *s)
251
+static int gif_parse_next_image(GifState *s, int *got_picture)
252 252
 {
253
+    int ret;
254
+    *got_picture = sizeof(AVPicture);
253 255
     while (s->bytestream < s->bytestream_end) {
254 256
         int code = bytestream_get_byte(&s->bytestream);
255 257
 
256 258
         av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
257 259
 
258 260
         switch (code) {
259
-        case ',':
261
+        case GIF_IMAGE_SEPARATOR:
260 262
             return gif_read_image(s);
261
-        case '!':
262
-            if (gif_read_extension(s) < 0)
263
-                return -1;
263
+        case GIF_EXTENSION_INTRODUCER:
264
+            if ((ret = gif_read_extension(s)) < 0)
265
+                return ret;
264 266
             break;
265
-        case ';':
267
+        case GIF_TRAILER:
266 268
             /* end of image */
269
+            *got_picture = 0;
270
+            return 0;
267 271
         default:
268
-            /* error or erroneous EOF */
269
-            return -1;
272
+            /* erroneous block label */
273
+            return AVERROR_INVALIDDATA;
270 274
         }
271 275
     }
272
-    return -1;
276
+    return AVERROR_EOF;
273 277
 }
274 278
 
275 279
 static av_cold int gif_decode_init(AVCodecContext *avctx)
... ...
@@ -285,7 +293,7 @@ static av_cold int gif_decode_init(AVCodecContext *avctx)
285 285
     return 0;
286 286
 }
287 287
 
288
-static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
288
+static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_picture, AVPacket *avpkt)
289 289
 {
290 290
     const uint8_t *buf = avpkt->data;
291 291
     int buf_size = avpkt->size;
... ...
@@ -299,8 +307,8 @@ static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, A
299 299
         return ret;
300 300
 
301 301
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
302
-    if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
303
-        return -1;
302
+    if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
303
+        return ret;
304 304
     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
305 305
 
306 306
     if (s->picture.data[0])
... ...
@@ -310,12 +318,12 @@ static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, A
310 310
         return ret;
311 311
     }
312 312
     s->image_palette = (uint32_t *)s->picture.data[1];
313
-    ret = gif_parse_next_image(s);
313
+    ret = gif_parse_next_image(s, got_picture);
314 314
     if (ret < 0)
315 315
         return ret;
316
+    else if (*got_picture)
317
+        *picture = s->picture;
316 318
 
317
-    *picture = s->picture;
318
-    *data_size = sizeof(AVPicture);
319 319
     return s->bytestream - buf;
320 320
 }
321 321