]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvbsubdec.c
always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
[ffmpeg] / libavcodec / dvbsubdec.c
1 /*
2  * DVB subtitle decoding for ffmpeg
3  * Copyright (c) 2005 Ian Caulfield.
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 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "bitstream.h"
24
25 //#define DEBUG
26 //#define DEBUG_PACKET_CONTENTS
27 //#define DEBUG_SAVE_IMAGES
28
29 #define DVBSUB_PAGE_SEGMENT     0x10
30 #define DVBSUB_REGION_SEGMENT   0x11
31 #define DVBSUB_CLUT_SEGMENT     0x12
32 #define DVBSUB_OBJECT_SEGMENT   0x13
33 #define DVBSUB_DISPLAY_SEGMENT  0x80
34
35 static unsigned char *cm;
36
37 #ifdef DEBUG_SAVE_IMAGES
38 #undef fprintf
39 #if 0
40 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
41                      uint32_t *rgba_palette)
42 {
43     int x, y, v;
44     FILE *f;
45     char fname[40], fname2[40];
46     char command[1024];
47
48     snprintf(fname, 40, "%s.ppm", filename);
49
50     f = fopen(fname, "w");
51     if (!f) {
52         perror(fname);
53         exit(1);
54     }
55     fprintf(f, "P6\n"
56             "%d %d\n"
57             "%d\n",
58             w, h, 255);
59     for(y = 0; y < h; y++) {
60         for(x = 0; x < w; x++) {
61             v = rgba_palette[bitmap[y * w + x]];
62             putc((v >> 16) & 0xff, f);
63             putc((v >> 8) & 0xff, f);
64             putc((v >> 0) & 0xff, f);
65         }
66     }
67     fclose(f);
68
69
70     snprintf(fname2, 40, "%s-a.pgm", filename);
71
72     f = fopen(fname2, "w");
73     if (!f) {
74         perror(fname2);
75         exit(1);
76     }
77     fprintf(f, "P5\n"
78             "%d %d\n"
79             "%d\n",
80             w, h, 255);
81     for(y = 0; y < h; y++) {
82         for(x = 0; x < w; x++) {
83             v = rgba_palette[bitmap[y * w + x]];
84             putc((v >> 24) & 0xff, f);
85         }
86     }
87     fclose(f);
88
89     snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90     system(command);
91
92     snprintf(command, 1024, "rm %s %s", fname, fname2);
93     system(command);
94 }
95 #endif
96
97 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
98 {
99     int x, y, v;
100     FILE *f;
101     char fname[40], fname2[40];
102     char command[1024];
103
104     snprintf(fname, 40, "%s.ppm", filename);
105
106     f = fopen(fname, "w");
107     if (!f) {
108         perror(fname);
109         exit(1);
110     }
111     fprintf(f, "P6\n"
112             "%d %d\n"
113             "%d\n",
114             w, h, 255);
115     for(y = 0; y < h; y++) {
116         for(x = 0; x < w; x++) {
117             v = bitmap[y * w + x];
118             putc((v >> 16) & 0xff, f);
119             putc((v >> 8) & 0xff, f);
120             putc((v >> 0) & 0xff, f);
121         }
122     }
123     fclose(f);
124
125
126     snprintf(fname2, 40, "%s-a.pgm", filename);
127
128     f = fopen(fname2, "w");
129     if (!f) {
130         perror(fname2);
131         exit(1);
132     }
133     fprintf(f, "P5\n"
134             "%d %d\n"
135             "%d\n",
136             w, h, 255);
137     for(y = 0; y < h; y++) {
138         for(x = 0; x < w; x++) {
139             v = bitmap[y * w + x];
140             putc((v >> 24) & 0xff, f);
141         }
142     }
143     fclose(f);
144
145     snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
146     system(command);
147
148     snprintf(command, 1024, "rm %s %s", fname, fname2);
149     system(command);
150 }
151 #endif
152
153 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
154
155 typedef struct DVBSubCLUT {
156     int id;
157
158     uint32_t clut4[4];
159     uint32_t clut16[16];
160     uint32_t clut256[256];
161
162     struct DVBSubCLUT *next;
163 } DVBSubCLUT;
164
165 static DVBSubCLUT default_clut;
166
167 typedef struct DVBSubObjectDisplay {
168     int object_id;
169     int region_id;
170
171     int x_pos;
172     int y_pos;
173
174     int fgcolour;
175     int bgcolour;
176
177     struct DVBSubObjectDisplay *region_list_next;
178     struct DVBSubObjectDisplay *object_list_next;
179 } DVBSubObjectDisplay;
180
181 typedef struct DVBSubObject {
182     int id;
183
184     int type;
185
186     DVBSubObjectDisplay *display_list;
187
188     struct DVBSubObject *next;
189 } DVBSubObject;
190
191 typedef struct DVBSubRegionDisplay {
192     int region_id;
193
194     int x_pos;
195     int y_pos;
196
197     struct DVBSubRegionDisplay *next;
198 } DVBSubRegionDisplay;
199
200 typedef struct DVBSubRegion {
201     int id;
202
203     int width;
204     int height;
205     int depth;
206
207     int clut;
208     int bgcolour;
209
210     uint8_t *pbuf;
211     int buf_size;
212
213     DVBSubObjectDisplay *display_list;
214
215     struct DVBSubRegion *next;
216 } DVBSubRegion;
217
218 typedef struct DVBSubContext {
219     int composition_id;
220     int ancillary_id;
221
222     int time_out;
223     DVBSubRegion *region_list;
224     DVBSubCLUT   *clut_list;
225     DVBSubObject *object_list;
226
227     int display_list_size;
228     DVBSubRegionDisplay *display_list;
229 } DVBSubContext;
230
231
232 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
233 {
234     DVBSubObject *ptr = ctx->object_list;
235
236     while (ptr != NULL && ptr->id != object_id) {
237         ptr = ptr->next;
238     }
239
240     return ptr;
241 }
242
243 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
244 {
245     DVBSubCLUT *ptr = ctx->clut_list;
246
247     while (ptr != NULL && ptr->id != clut_id) {
248         ptr = ptr->next;
249     }
250
251     return ptr;
252 }
253
254 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
255 {
256     DVBSubRegion *ptr = ctx->region_list;
257
258     while (ptr != NULL && ptr->id != region_id) {
259         ptr = ptr->next;
260     }
261
262     return ptr;
263 }
264
265 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
266 {
267     DVBSubObject *object, *obj2, **obj2_ptr;
268     DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
269
270     while (region->display_list != NULL) {
271         display = region->display_list;
272
273         object = get_object(ctx, display->object_id);
274
275         if (object != NULL) {
276             obj_disp = object->display_list;
277             obj_disp_ptr = &object->display_list;
278
279             while (obj_disp != NULL && obj_disp != display) {
280                 obj_disp_ptr = &obj_disp->object_list_next;
281                 obj_disp = obj_disp->object_list_next;
282             }
283
284             if (obj_disp) {
285                 *obj_disp_ptr = obj_disp->object_list_next;
286
287                 if (object->display_list == NULL) {
288                     obj2 = ctx->object_list;
289                     obj2_ptr = &ctx->object_list;
290
291                     while (obj2 != NULL && obj2 != object) {
292                         obj2_ptr = &obj2->next;
293                         obj2 = obj2->next;
294                     }
295
296                     *obj2_ptr = obj2->next;
297
298                     av_free(obj2);
299                 }
300             }
301         }
302
303         region->display_list = display->region_list_next;
304
305         av_free(display);
306     }
307
308 }
309
310 static void delete_state(DVBSubContext *ctx)
311 {
312     DVBSubRegion *region;
313     DVBSubCLUT *clut;
314
315     while (ctx->region_list != NULL)
316     {
317         region = ctx->region_list;
318
319         ctx->region_list = region->next;
320
321         delete_region_display_list(ctx, region);
322         if (region->pbuf != NULL)
323             av_free(region->pbuf);
324
325         av_free(region);
326     }
327
328     while (ctx->clut_list != NULL)
329     {
330         clut = ctx->clut_list;
331
332         ctx->clut_list = clut->next;
333
334         av_free(clut);
335     }
336
337     /* Should already be null */
338     if (ctx->object_list != NULL)
339         av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
340 }
341
342 static int dvbsub_init_decoder(AVCodecContext *avctx)
343 {
344     int i, r, g, b, a = 0;
345     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
346
347     cm = ff_cropTbl + MAX_NEG_CROP;
348
349     memset(avctx->priv_data, 0, sizeof(DVBSubContext));
350
351     ctx->composition_id = avctx->sub_id & 0xffff;
352     ctx->ancillary_id = avctx->sub_id >> 16;
353
354     default_clut.id = -1;
355     default_clut.next = NULL;
356
357     default_clut.clut4[0] = RGBA(  0,   0,   0,   0);
358     default_clut.clut4[1] = RGBA(255, 255, 255, 255);
359     default_clut.clut4[2] = RGBA(  0,   0,   0, 255);
360     default_clut.clut4[3] = RGBA(127, 127, 127, 255);
361
362     default_clut.clut16[0] = RGBA(  0,   0,   0,   0);
363     for (i = 1; i < 16; i++) {
364         if (i < 8) {
365             r = (i & 1) ? 255 : 0;
366             g = (i & 2) ? 255 : 0;
367             b = (i & 4) ? 255 : 0;
368         } else {
369             r = (i & 1) ? 127 : 0;
370             g = (i & 2) ? 127 : 0;
371             b = (i & 4) ? 127 : 0;
372         }
373         default_clut.clut16[i] = RGBA(r, g, b, 255);
374     }
375
376     default_clut.clut256[0] = RGBA(  0,   0,   0,   0);
377     for (i = 1; i < 256; i++) {
378         if (i < 8) {
379             r = (i & 1) ? 255 : 0;
380             g = (i & 2) ? 255 : 0;
381             b = (i & 4) ? 255 : 0;
382             a = 63;
383         } else {
384             switch (i & 0x88) {
385             case 0x00:
386                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
387                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
388                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
389                 a = 255;
390                 break;
391             case 0x08:
392                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
393                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
394                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
395                 a = 127;
396                 break;
397             case 0x80:
398                 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
399                 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
400                 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
401                 a = 255;
402                 break;
403             case 0x88:
404                 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
405                 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
406                 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
407                 a = 255;
408                 break;
409             }
410         }
411         default_clut.clut256[i] = RGBA(r, g, b, a);
412     }
413
414     return 0;
415 }
416
417 static int dvbsub_close_decoder(AVCodecContext *avctx)
418 {
419     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
420     DVBSubRegionDisplay *display;
421
422     delete_state(ctx);
423
424     while (ctx->display_list != NULL)
425     {
426         display = ctx->display_list;
427         ctx->display_list = display->next;
428
429         av_free(display);
430     }
431
432     return 0;
433 }
434
435 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
436                                    uint8_t **srcbuf, int buf_size,
437                                    int non_mod, uint8_t *map_table)
438 {
439     GetBitContext gb;
440
441     int bits;
442     int run_length;
443     int pixels_read = 0;
444
445     init_get_bits(&gb, *srcbuf, buf_size << 8);
446
447     while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
448         bits = get_bits(&gb, 2);
449
450         if (bits != 0) {
451             if (non_mod != 1 || bits != 1) {
452                 if (map_table != NULL)
453                     *destbuf++ = map_table[bits];
454                 else
455                     *destbuf++ = bits;
456             }
457             pixels_read++;
458         } else {
459             bits = get_bits(&gb, 1);
460             if (bits == 1) {
461                 run_length = get_bits(&gb, 3) + 3;
462                 bits = get_bits(&gb, 2);
463
464                 if (non_mod == 1 && bits == 1)
465                     pixels_read += run_length;
466                 else {
467                     if (map_table != NULL)
468                         bits = map_table[bits];
469                     while (run_length-- > 0 && pixels_read < dbuf_len) {
470                         *destbuf++ = bits;
471                         pixels_read++;
472                     }
473                 }
474             } else {
475                 bits = get_bits(&gb, 1);
476                 if (bits == 0) {
477                     bits = get_bits(&gb, 2);
478                     if (bits == 2) {
479                         run_length = get_bits(&gb, 4) + 12;
480                         bits = get_bits(&gb, 2);
481
482                         if (non_mod == 1 && bits == 1)
483                             pixels_read += run_length;
484                         else {
485                             if (map_table != NULL)
486                                 bits = map_table[bits];
487                             while (run_length-- > 0 && pixels_read < dbuf_len) {
488                                 *destbuf++ = bits;
489                                 pixels_read++;
490                             }
491                         }
492                     } else if (bits == 3) {
493                         run_length = get_bits(&gb, 8) + 29;
494                         bits = get_bits(&gb, 2);
495
496                         if (non_mod == 1 && bits == 1)
497                             pixels_read += run_length;
498                         else {
499                             if (map_table != NULL)
500                                 bits = map_table[bits];
501                             while (run_length-- > 0 && pixels_read < dbuf_len) {
502                                 *destbuf++ = bits;
503                                 pixels_read++;
504                             }
505                         }
506                     } else if (bits == 1) {
507                         pixels_read += 2;
508                         if (map_table != NULL)
509                             bits = map_table[0];
510                         else
511                             bits = 0;
512                         if (pixels_read <= dbuf_len) {
513                             *destbuf++ = bits;
514                             *destbuf++ = bits;
515                         }
516                     } else {
517                         (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
518                         return pixels_read;
519                     }
520                 } else {
521                     if (map_table != NULL)
522                         bits = map_table[0];
523                     else
524                         bits = 0;
525                     *destbuf++ = bits;
526                     pixels_read++;
527                 }
528             }
529         }
530     }
531
532     if (get_bits(&gb, 6) != 0)
533         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
534
535     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
536
537     return pixels_read;
538 }
539
540 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
541                                    uint8_t **srcbuf, int buf_size,
542                                    int non_mod, uint8_t *map_table)
543 {
544     GetBitContext gb;
545
546     int bits;
547     int run_length;
548     int pixels_read = 0;
549
550     init_get_bits(&gb, *srcbuf, buf_size << 8);
551
552     while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
553         bits = get_bits(&gb, 4);
554
555         if (bits != 0) {
556             if (non_mod != 1 || bits != 1) {
557                 if (map_table != NULL)
558                     *destbuf++ = map_table[bits];
559                 else
560                     *destbuf++ = bits;
561             }
562             pixels_read++;
563         } else {
564             bits = get_bits(&gb, 1);
565             if (bits == 0) {
566                 run_length = get_bits(&gb, 3);
567
568                 if (run_length == 0) {
569                     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
570                     return pixels_read;
571                 }
572
573                 run_length += 2;
574
575                 if (map_table != NULL)
576                     bits = map_table[0];
577                 else
578                     bits = 0;
579
580                 while (run_length-- > 0 && pixels_read < dbuf_len) {
581                     *destbuf++ = bits;
582                     pixels_read++;
583                 }
584             } else {
585                 bits = get_bits(&gb, 1);
586                 if (bits == 0) {
587                     run_length = get_bits(&gb, 2) + 4;
588                     bits = get_bits(&gb, 4);
589
590                     if (non_mod == 1 && bits == 1)
591                         pixels_read += run_length;
592                     else {
593                         if (map_table != NULL)
594                             bits = map_table[bits];
595                         while (run_length-- > 0 && pixels_read < dbuf_len) {
596                             *destbuf++ = bits;
597                             pixels_read++;
598                         }
599                     }
600                 } else {
601                     bits = get_bits(&gb, 2);
602                     if (bits == 2) {
603                         run_length = get_bits(&gb, 4) + 9;
604                         bits = get_bits(&gb, 4);
605
606                         if (non_mod == 1 && bits == 1)
607                             pixels_read += run_length;
608                         else {
609                             if (map_table != NULL)
610                                 bits = map_table[bits];
611                             while (run_length-- > 0 && pixels_read < dbuf_len) {
612                                 *destbuf++ = bits;
613                                 pixels_read++;
614                             }
615                         }
616                     } else if (bits == 3) {
617                         run_length = get_bits(&gb, 8) + 25;
618                         bits = get_bits(&gb, 4);
619
620                         if (non_mod == 1 && bits == 1)
621                             pixels_read += run_length;
622                         else {
623                             if (map_table != NULL)
624                                 bits = map_table[bits];
625                             while (run_length-- > 0 && pixels_read < dbuf_len) {
626                                 *destbuf++ = bits;
627                                 pixels_read++;
628                             }
629                         }
630                     } else if (bits == 1) {
631                         pixels_read += 2;
632                         if (map_table != NULL)
633                             bits = map_table[0];
634                         else
635                             bits = 0;
636                         if (pixels_read <= dbuf_len) {
637                             *destbuf++ = bits;
638                             *destbuf++ = bits;
639                         }
640                     } else {
641                         if (map_table != NULL)
642                             bits = map_table[0];
643                         else
644                             bits = 0;
645                         *destbuf++ = bits;
646                         pixels_read ++;
647                     }
648                 }
649             }
650         }
651     }
652
653     if (get_bits(&gb, 8) != 0)
654         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
655
656     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
657
658     return pixels_read;
659 }
660
661 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
662                                     uint8_t **srcbuf, int buf_size,
663                                     int non_mod, uint8_t *map_table)
664 {
665     uint8_t *sbuf_end = (*srcbuf) + buf_size;
666     int bits;
667     int run_length;
668     int pixels_read = 0;
669
670     while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
671         bits = *(*srcbuf)++;
672
673         if (bits != 0) {
674             if (non_mod != 1 || bits != 1) {
675                 if (map_table != NULL)
676                     *destbuf++ = map_table[bits];
677                 else
678                     *destbuf++ = bits;
679             }
680             pixels_read++;
681         } else {
682             bits = *(*srcbuf)++;
683             run_length = bits & 0x7f;
684             if ((bits & 0x80) == 0) {
685                 if (run_length == 0) {
686                     return pixels_read;
687                 }
688
689                 if (map_table != NULL)
690                     bits = map_table[0];
691                 else
692                     bits = 0;
693                 while (run_length-- > 0 && pixels_read < dbuf_len) {
694                     *destbuf++ = bits;
695                     pixels_read++;
696                 }
697             } else {
698                 bits = *(*srcbuf)++;
699
700                 if (non_mod == 1 && bits == 1)
701                     pixels_read += run_length;
702                 if (map_table != NULL)
703                     bits = map_table[bits];
704                 else while (run_length-- > 0 && pixels_read < dbuf_len) {
705                     *destbuf++ = bits;
706                     pixels_read++;
707                 }
708             }
709         }
710     }
711
712     if (*(*srcbuf)++ != 0)
713         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
714
715     return pixels_read;
716 }
717
718
719
720 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
721                                           uint8_t *buf, int buf_size, int top_bottom, int non_mod)
722 {
723     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
724
725     DVBSubRegion *region = get_region(ctx, display->region_id);
726     uint8_t *buf_end = buf + buf_size;
727     uint8_t *pbuf;
728     int x_pos, y_pos;
729     int i;
730
731     uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
732     uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
733     uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
734                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
735     uint8_t *map_table;
736
737 #ifdef DEBUG
738     av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
739                 top_bottom ? "bottom" : "top");
740 #endif
741
742 #ifdef DEBUG_PACKET_CONTENTS
743     for (i = 0; i < buf_size; i++)
744     {
745         if (i % 16 == 0)
746             av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
747
748         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
749         if (i % 16 == 15)
750             av_log(avctx, AV_LOG_INFO, "\n");
751     }
752
753     if (i % 16 != 0)
754         av_log(avctx, AV_LOG_INFO, "\n");
755
756 #endif
757
758     if (region == 0)
759         return;
760
761     pbuf = region->pbuf;
762
763     x_pos = display->x_pos;
764     y_pos = display->y_pos;
765
766     if ((y_pos & 1) != top_bottom)
767         y_pos++;
768
769     while (buf < buf_end) {
770         if (x_pos > region->width || y_pos > region->height) {
771             av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
772             return;
773         }
774
775         switch (*buf++) {
776         case 0x10:
777             if (region->depth == 8)
778                 map_table = map2to8;
779             else if (region->depth == 4)
780                 map_table = map2to4;
781             else
782                 map_table = NULL;
783
784             x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
785                                                 region->width - x_pos, &buf, buf_size,
786                                                 non_mod, map_table);
787             break;
788         case 0x11:
789             if (region->depth < 4) {
790                 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
791                 return;
792             }
793
794             if (region->depth == 8)
795                 map_table = map4to8;
796             else
797                 map_table = NULL;
798
799             x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
800                                                 region->width - x_pos, &buf, buf_size,
801                                                 non_mod, map_table);
802             break;
803         case 0x12:
804             if (region->depth < 8) {
805                 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
806                 return;
807             }
808
809             x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
810                                                 region->width - x_pos, &buf, buf_size,
811                                                 non_mod, NULL);
812             break;
813
814         case 0x20:
815             map2to4[0] = (*buf) >> 4;
816             map2to4[1] = (*buf++) & 0xf;
817             map2to4[2] = (*buf) >> 4;
818             map2to4[3] = (*buf++) & 0xf;
819             break;
820         case 0x21:
821             for (i = 0; i < 4; i++)
822                 map2to8[i] = *buf++;
823             break;
824         case 0x22:
825             for (i = 0; i < 16; i++)
826                 map4to8[i] = *buf++;
827             break;
828
829         case 0xf0:
830             x_pos = display->x_pos;
831             y_pos += 2;
832             break;
833         default:
834             av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
835         }
836     }
837
838 }
839
840 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
841                                         uint8_t *buf, int buf_size)
842 {
843     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
844
845     uint8_t *buf_end = buf + buf_size;
846     uint8_t *block;
847     int object_id;
848     DVBSubObject *object;
849     DVBSubObjectDisplay *display;
850     int top_field_len, bottom_field_len;
851
852     int coding_method, non_modifying_colour;
853
854     object_id = BE_16(buf);
855     buf += 2;
856
857     object = get_object(ctx, object_id);
858
859     if (!object)
860         return;
861
862     coding_method = ((*buf) >> 2) & 3;
863     non_modifying_colour = ((*buf++) >> 1) & 1;
864
865     if (coding_method == 0) {
866         top_field_len = BE_16(buf);
867         buf += 2;
868         bottom_field_len = BE_16(buf);
869         buf += 2;
870
871         if (buf + top_field_len + bottom_field_len > buf_end) {
872             av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
873             return;
874         }
875
876         for (display = object->display_list; display != 0; display = display->object_list_next) {
877             block = buf;
878
879             dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
880                                             non_modifying_colour);
881
882             if (bottom_field_len > 0)
883                 block = buf + top_field_len;
884             else
885                 bottom_field_len = top_field_len;
886
887             dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
888                                             non_modifying_colour);
889         }
890
891 /*  } else if (coding_method == 1) {*/
892
893     } else {
894         av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
895     }
896
897 }
898
899 #define SCALEBITS 10
900 #define ONE_HALF  (1 << (SCALEBITS - 1))
901 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
902
903 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
904 {\
905     cb = (cb1) - 128;\
906     cr = (cr1) - 128;\
907     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
908     g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
909             ONE_HALF;\
910     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
911 }
912
913 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
914 {\
915     y = ((y1) - 16) * FIX(255.0/219.0);\
916     r = cm[(y + r_add) >> SCALEBITS];\
917     g = cm[(y + g_add) >> SCALEBITS];\
918     b = cm[(y + b_add) >> SCALEBITS];\
919 }
920
921
922 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
923                                         uint8_t *buf, int buf_size)
924 {
925     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
926
927     uint8_t *buf_end = buf + buf_size;
928     int clut_id;
929     DVBSubCLUT *clut;
930     int entry_id, depth , full_range;
931     int y, cr, cb, alpha;
932     int r, g, b, r_add, g_add, b_add;
933
934 #ifdef DEBUG_PACKET_CONTENTS
935     int i;
936
937     av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
938
939     for (i=0; i < buf_size; i++)
940     {
941         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
942         if (i % 16 == 15)
943             av_log(avctx, AV_LOG_INFO, "\n");
944     }
945
946     if (i % 16 != 0)
947         av_log(avctx, AV_LOG_INFO, "\n");
948
949 #endif
950
951     clut_id = *buf++;
952     buf += 1;
953
954     clut = get_clut(ctx, clut_id);
955
956     if (clut == NULL) {
957         clut = av_malloc(sizeof(DVBSubCLUT));
958
959         memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
960
961         clut->id = clut_id;
962
963         clut->next = ctx->clut_list;
964         ctx->clut_list = clut;
965     }
966
967     while (buf + 4 < buf_end)
968     {
969         entry_id = *buf++;
970
971         depth = (*buf) & 0xe0;
972
973         if (depth == 0) {
974             av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
975             return;
976         }
977
978         full_range = (*buf++) & 1;
979
980         if (full_range) {
981             y = *buf++;
982             cr = *buf++;
983             cb = *buf++;
984             alpha = *buf++;
985         } else {
986             y = buf[0] & 0xfc;
987             cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
988             cb = (buf[1] << 2) & 0xf0;
989             alpha = (buf[1] << 6) & 0xc0;
990
991             buf += 2;
992         }
993
994         if (y == 0)
995             alpha = 0xff;
996
997         YUV_TO_RGB1_CCIR(cb, cr);
998         YUV_TO_RGB2_CCIR(r, g, b, y);
999
1000 #ifdef DEBUG
1001         av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1002 #endif
1003
1004         if (depth & 0x80)
1005             clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1006         if (depth & 0x40)
1007             clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1008         if (depth & 0x20)
1009             clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1010     }
1011 }
1012
1013
1014 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1015                                         uint8_t *buf, int buf_size)
1016 {
1017     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1018
1019     uint8_t *buf_end = buf + buf_size;
1020     int region_id, object_id;
1021     DVBSubRegion *region;
1022     DVBSubObject *object;
1023     DVBSubObjectDisplay *display;
1024     int fill;
1025
1026     if (buf_size < 10)
1027         return;
1028
1029     region_id = *buf++;
1030
1031     region = get_region(ctx, region_id);
1032
1033     if (region == NULL)
1034     {
1035         region = av_mallocz(sizeof(DVBSubRegion));
1036
1037         region->id = region_id;
1038
1039         region->next = ctx->region_list;
1040         ctx->region_list = region;
1041     }
1042
1043     fill = ((*buf++) >> 3) & 1;
1044
1045     region->width = BE_16(buf);
1046     buf += 2;
1047     region->height = BE_16(buf);
1048     buf += 2;
1049
1050     if (region->width * region->height != region->buf_size) {
1051         if (region->pbuf != 0)
1052             av_free(region->pbuf);
1053
1054         region->buf_size = region->width * region->height;
1055
1056         region->pbuf = av_malloc(region->buf_size);
1057
1058         fill = 1;
1059     }
1060
1061     region->depth = 1 << (((*buf++) >> 2) & 7);
1062     region->clut = *buf++;
1063
1064     if (region->depth == 8)
1065         region->bgcolour = *buf++;
1066     else {
1067         buf += 1;
1068
1069         if (region->depth == 4)
1070             region->bgcolour = (((*buf++) >> 4) & 15);
1071         else
1072             region->bgcolour = (((*buf++) >> 2) & 3);
1073     }
1074
1075 #ifdef DEBUG
1076     av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1077 #endif
1078
1079     if (fill) {
1080         memset(region->pbuf, region->bgcolour, region->buf_size);
1081 #ifdef DEBUG
1082         av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolour);
1083 #endif
1084     }
1085
1086     delete_region_display_list(ctx, region);
1087
1088     while (buf + 5 < buf_end) {
1089         object_id = BE_16(buf);
1090         buf += 2;
1091
1092         object = get_object(ctx, object_id);
1093
1094         if (object == NULL) {
1095             object = av_mallocz(sizeof(DVBSubObject));
1096
1097             object->id = object_id;
1098             object->next = ctx->object_list;
1099             ctx->object_list = object;
1100         }
1101
1102         object->type = (*buf) >> 6;
1103
1104         display = av_mallocz(sizeof(DVBSubObjectDisplay));
1105
1106         display->object_id = object_id;
1107         display->region_id = region_id;
1108
1109         display->x_pos = BE_16(buf) & 0xfff;
1110         buf += 2;
1111         display->y_pos = BE_16(buf) & 0xfff;
1112         buf += 2;
1113
1114         if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1115             display->fgcolour = *buf++;
1116             display->bgcolour = *buf++;
1117         }
1118
1119         display->region_list_next = region->display_list;
1120         region->display_list = display;
1121
1122         display->object_list_next = object->display_list;
1123         object->display_list = display;
1124     }
1125 }
1126
1127 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1128                                         uint8_t *buf, int buf_size)
1129 {
1130     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1131     DVBSubRegionDisplay *display;
1132     DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1133
1134     uint8_t *buf_end = buf + buf_size;
1135     int region_id;
1136     int page_state;
1137
1138     if (buf_size < 1)
1139         return;
1140
1141     ctx->time_out = *buf++;
1142     page_state = ((*buf++) >> 2) & 3;
1143
1144 #ifdef DEBUG
1145     av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1146 #endif
1147
1148     if (page_state == 2)
1149     {
1150         delete_state(ctx);
1151     }
1152
1153     tmp_display_list = ctx->display_list;
1154     ctx->display_list = NULL;
1155     ctx->display_list_size = 0;
1156
1157     while (buf + 5 < buf_end) {
1158         region_id = *buf++;
1159         buf += 1;
1160
1161         display = tmp_display_list;
1162         tmp_ptr = &tmp_display_list;
1163
1164         while (display != NULL && display->region_id != region_id) {
1165             tmp_ptr = &display->next;
1166             display = display->next;
1167         }
1168
1169         if (display == NULL)
1170             display = av_mallocz(sizeof(DVBSubRegionDisplay));
1171
1172         display->region_id = region_id;
1173
1174         display->x_pos = BE_16(buf);
1175         buf += 2;
1176         display->y_pos = BE_16(buf);
1177         buf += 2;
1178
1179         *tmp_ptr = display->next;
1180
1181         display->next = ctx->display_list;
1182         ctx->display_list = display;
1183         ctx->display_list_size++;
1184
1185 #ifdef DEBUG
1186         av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1187 #endif
1188     }
1189
1190     while (tmp_display_list != 0) {
1191         display = tmp_display_list;
1192
1193         tmp_display_list = display->next;
1194
1195         av_free(display);
1196     }
1197
1198 }
1199
1200
1201 #ifdef DEBUG_SAVE_IMAGES
1202 static void save_display_set(DVBSubContext *ctx)
1203 {
1204     DVBSubRegion *region;
1205     DVBSubRegionDisplay *display;
1206     DVBSubCLUT *clut;
1207     uint32_t *clut_table;
1208     int x_pos, y_pos, width, height;
1209     int x, y, y_off, x_off;
1210     uint32_t *pbuf;
1211     char filename[32];
1212     static int fileno_index = 0;
1213
1214     x_pos = -1;
1215     y_pos = -1;
1216     width = 0;
1217     height = 0;
1218
1219     for (display = ctx->display_list; display != NULL; display = display->next) {
1220         region = get_region(ctx, display->region_id);
1221
1222         if (x_pos == -1) {
1223             x_pos = display->x_pos;
1224             y_pos = display->y_pos;
1225             width = region->width;
1226             height = region->height;
1227         } else {
1228             if (display->x_pos < x_pos) {
1229                 width += (x_pos - display->x_pos);
1230                 x_pos = display->x_pos;
1231             }
1232
1233             if (display->y_pos < y_pos) {
1234                 height += (y_pos - display->y_pos);
1235                 y_pos = display->y_pos;
1236             }
1237
1238             if (display->x_pos + region->width > x_pos + width) {
1239                 width = display->x_pos + region->width - x_pos;
1240             }
1241
1242             if (display->y_pos + region->height > y_pos + height) {
1243                 height = display->y_pos + region->height - y_pos;
1244             }
1245         }
1246     }
1247
1248     if (x_pos >= 0) {
1249
1250         pbuf = av_malloc(width * height * 4);
1251
1252         for (display = ctx->display_list; display != NULL; display = display->next) {
1253             region = get_region(ctx, display->region_id);
1254
1255             x_off = display->x_pos - x_pos;
1256             y_off = display->y_pos - y_pos;
1257
1258             clut = get_clut(ctx, region->clut);
1259
1260             if (clut == 0)
1261                 clut = &default_clut;
1262
1263             switch (region->depth) {
1264             case 2:
1265                 clut_table = clut->clut4;
1266                 break;
1267             case 8:
1268                 clut_table = clut->clut256;
1269                 break;
1270             case 4:
1271             default:
1272                 clut_table = clut->clut16;
1273                 break;
1274             }
1275
1276             for (y = 0; y < region->height; y++) {
1277                 for (x = 0; x < region->width; x++) {
1278                     pbuf[((y + y_off) * width) + x_off + x] =
1279                         clut_table[region->pbuf[y * region->width + x]];
1280                 }
1281             }
1282
1283         }
1284
1285         snprintf(filename, 32, "dvbs.%d", fileno_index);
1286
1287         png_save2(filename, pbuf, width, height);
1288
1289         av_free(pbuf);
1290     }
1291
1292     fileno_index++;
1293 }
1294 #endif
1295
1296 static int dvbsub_display_end_segment(AVCodecContext *avctx, uint8_t *buf,
1297                                         int buf_size, AVSubtitle *sub)
1298 {
1299     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1300
1301     DVBSubRegion *region;
1302     DVBSubRegionDisplay *display;
1303     AVSubtitleRect *rect;
1304     DVBSubCLUT *clut;
1305     uint32_t *clut_table;
1306     int i;
1307
1308     sub->rects = NULL;
1309     sub->start_display_time = 0;
1310     sub->end_display_time = ctx->time_out * 1000;
1311     sub->format = 0;
1312
1313     sub->num_rects = ctx->display_list_size;
1314
1315     if (sub->num_rects > 0)
1316         sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
1317
1318     i = 0;
1319
1320     for (display = ctx->display_list; display != NULL; display = display->next) {
1321         region = get_region(ctx, display->region_id);
1322         rect = &sub->rects[i];
1323
1324         if (region == NULL)
1325             continue;
1326
1327         rect->x = display->x_pos;
1328         rect->y = display->y_pos;
1329         rect->w = region->width;
1330         rect->h = region->height;
1331         rect->nb_colors = 16;
1332         rect->linesize = region->width;
1333
1334         clut = get_clut(ctx, region->clut);
1335
1336         if (clut == NULL)
1337             clut = &default_clut;
1338
1339         switch (region->depth) {
1340         case 2:
1341             clut_table = clut->clut4;
1342             break;
1343         case 8:
1344             clut_table = clut->clut256;
1345             break;
1346         case 4:
1347         default:
1348             clut_table = clut->clut16;
1349             break;
1350         }
1351
1352         rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
1353         memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
1354
1355         rect->bitmap = av_malloc(region->buf_size);
1356         memcpy(rect->bitmap, region->pbuf, region->buf_size);
1357
1358         i++;
1359     }
1360
1361     sub->num_rects = i;
1362
1363 #ifdef DEBUG_SAVE_IMAGES
1364     save_display_set(ctx);
1365 #endif
1366
1367     return 1;
1368 }
1369
1370 static int dvbsub_decode(AVCodecContext *avctx,
1371                          void *data, int *data_size,
1372                          uint8_t *buf, int buf_size)
1373 {
1374     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1375     AVSubtitle *sub = (AVSubtitle*) data;
1376     uint8_t *p, *p_end;
1377     int segment_type;
1378     int page_id;
1379     int segment_length;
1380
1381 #ifdef DEBUG_PACKET_CONTENTS
1382     int i;
1383
1384     av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1385
1386     for (i=0; i < buf_size; i++)
1387     {
1388         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1389         if (i % 16 == 15)
1390             av_log(avctx, AV_LOG_INFO, "\n");
1391     }
1392
1393     if (i % 16 != 0)
1394         av_log(avctx, AV_LOG_INFO, "\n");
1395
1396 #endif
1397
1398     if (buf_size <= 2)
1399         return -1;
1400
1401     p = buf;
1402     p_end = buf + buf_size;
1403
1404     while (p < p_end && *p == 0x0f)
1405     {
1406         p += 1;
1407         segment_type = *p++;
1408         page_id = BE_16(p);
1409         p += 2;
1410         segment_length = BE_16(p);
1411         p += 2;
1412
1413         if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1414             switch (segment_type) {
1415             case DVBSUB_PAGE_SEGMENT:
1416                 dvbsub_parse_page_segment(avctx, p, segment_length);
1417                 break;
1418             case DVBSUB_REGION_SEGMENT:
1419                 dvbsub_parse_region_segment(avctx, p, segment_length);
1420                 break;
1421             case DVBSUB_CLUT_SEGMENT:
1422                 dvbsub_parse_clut_segment(avctx, p, segment_length);
1423                 break;
1424             case DVBSUB_OBJECT_SEGMENT:
1425                 dvbsub_parse_object_segment(avctx, p, segment_length);
1426                 break;
1427             case DVBSUB_DISPLAY_SEGMENT:
1428                 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1429                 break;
1430             default:
1431 #ifdef DEBUG
1432                 av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1433                         segment_type, page_id, segment_length);
1434 #endif
1435                 break;
1436             }
1437         }
1438
1439         p += segment_length;
1440     }
1441
1442     if (p != p_end)
1443     {
1444 #ifdef DEBUG
1445         av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1446 #endif
1447         return -1;
1448     }
1449
1450     return buf_size;
1451 }
1452
1453
1454 AVCodec dvbsub_decoder = {
1455     "dvbsub",
1456     CODEC_TYPE_SUBTITLE,
1457     CODEC_ID_DVB_SUBTITLE,
1458     sizeof(DVBSubContext),
1459     dvbsub_init_decoder,
1460     NULL,
1461     dvbsub_close_decoder,
1462     dvbsub_decode,
1463 };
1464
1465 /* Parser (mostly) copied from dvdsub.c */
1466
1467 #define PARSE_BUF_SIZE  (65536)
1468
1469
1470 /* parser definition */
1471 typedef struct DVBSubParseContext {
1472     uint8_t *packet_buf;
1473     int packet_start;
1474     int packet_index;
1475     int in_packet;
1476 } DVBSubParseContext;
1477
1478 static int dvbsub_parse_init(AVCodecParserContext *s)
1479 {
1480     DVBSubParseContext *pc = s->priv_data;
1481     pc->packet_buf = av_malloc(PARSE_BUF_SIZE);
1482
1483     return 0;
1484 }
1485
1486 static int dvbsub_parse(AVCodecParserContext *s,
1487                         AVCodecContext *avctx,
1488                         uint8_t **poutbuf, int *poutbuf_size,
1489                         const uint8_t *buf, int buf_size)
1490 {
1491     DVBSubParseContext *pc = s->priv_data;
1492     uint8_t *p, *p_end;
1493     int len, buf_pos = 0;
1494
1495 #ifdef DEBUG
1496     av_log(avctx, AV_LOG_INFO, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n",
1497             s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]);
1498 #endif
1499
1500 #ifdef DEBUG_PACKET_CONTENTS
1501     int i;
1502
1503     for (i=0; i < buf_size; i++)
1504     {
1505         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1506         if (i % 16 == 15)
1507             av_log(avctx, AV_LOG_INFO, "\n");
1508     }
1509
1510     if (i % 16 != 0)
1511         av_log(avctx, AV_LOG_INFO, "\n");
1512
1513 #endif
1514
1515     *poutbuf = NULL;
1516     *poutbuf_size = 0;
1517
1518     s->fetch_timestamp = 1;
1519
1520     if (s->last_pts != s->pts && s->last_pts != AV_NOPTS_VALUE) /* Start of a new packet */
1521     {
1522         if (pc->packet_index != pc->packet_start)
1523         {
1524 #ifdef DEBUG
1525             av_log(avctx, AV_LOG_INFO, "Discarding %d bytes\n",
1526                 pc->packet_index - pc->packet_start);
1527 #endif
1528         }
1529
1530         pc->packet_start = 0;
1531         pc->packet_index = 0;
1532
1533         if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
1534 #ifdef DEBUG
1535             av_log(avctx, AV_LOG_INFO, "Bad packet header\n");
1536 #endif
1537             return -1;
1538         }
1539
1540         buf_pos = 2;
1541
1542         pc->in_packet = 1;
1543     } else {
1544         if (pc->packet_start != 0)
1545         {
1546             if (pc->packet_index != pc->packet_start)
1547             {
1548                 memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
1549                             pc->packet_index - pc->packet_start);
1550
1551                 pc->packet_index -= pc->packet_start;
1552                 pc->packet_start = 0;
1553             } else {
1554                 pc->packet_start = 0;
1555                 pc->packet_index = 0;
1556             }
1557         }
1558     }
1559
1560     if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
1561         return -1;
1562
1563 /* if not currently in a packet, discard data */
1564     if (pc->in_packet == 0)
1565         return buf_size;
1566
1567     memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
1568     pc->packet_index += buf_size - buf_pos;
1569
1570     p = pc->packet_buf;
1571     p_end = pc->packet_buf + pc->packet_index;
1572
1573     while (p < p_end)
1574     {
1575         if (*p == 0x0f)
1576         {
1577             if (p + 6 <= p_end)
1578             {
1579                 len = BE_16(p + 4);
1580
1581                 if (p + len + 6 <= p_end)
1582                 {
1583                     *poutbuf_size += len + 6;
1584
1585                     p += len + 6;
1586                 } else
1587                     break;
1588             } else
1589                 break;
1590         } else if (*p == 0xff) {
1591             if (p + 1 < p_end)
1592             {
1593 #ifdef DEBUG
1594                 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1595 #endif
1596             }
1597             pc->packet_index = p - pc->packet_buf;
1598             pc->in_packet = 0;
1599             break;
1600         } else {
1601             av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
1602
1603             pc->packet_index = p - pc->packet_buf;
1604             pc->in_packet = 0;
1605             break;
1606         }
1607     }
1608
1609     if (*poutbuf_size > 0)
1610     {
1611         *poutbuf = pc->packet_buf;
1612         pc->packet_start = *poutbuf_size;
1613     }
1614
1615     if (s->last_pts == AV_NOPTS_VALUE)
1616         s->last_pts = s->pts;
1617
1618     return buf_size;
1619 }
1620
1621 static void dvbsub_parse_close(AVCodecParserContext *s)
1622 {
1623     DVBSubParseContext *pc = s->priv_data;
1624     av_freep(&pc->packet_buf);
1625 }
1626
1627 AVCodecParser dvbsub_parser = {
1628     { CODEC_ID_DVB_SUBTITLE },
1629     sizeof(DVBSubParseContext),
1630     dvbsub_parse_init,
1631     dvbsub_parse,
1632     dvbsub_parse_close,
1633 };