dvbsubdec.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:44k
源码类别:

Windows CE

开发平台:

C/C++

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