LCOV - code coverage report
Current view: top level - libavcodec - dvbsub.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 257 0.0 %
Date: 2017-12-15 18:13:28 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * DVB subtitle encoding
       3             :  * Copyright (c) 2005 Fabrice Bellard
       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 "bytestream.h"
      23             : #include "libavutil/colorspace.h"
      24             : 
      25             : typedef struct DVBSubtitleContext {
      26             :     int object_version;
      27             : } DVBSubtitleContext;
      28             : 
      29             : #define PUTBITS2(val)\
      30             : {\
      31             :     bitbuf |= (val) << bitcnt;\
      32             :     bitcnt -= 2;\
      33             :     if (bitcnt < 0) {\
      34             :         bitcnt = 6;\
      35             :         *q++ = bitbuf;\
      36             :         bitbuf = 0;\
      37             :     }\
      38             : }
      39             : 
      40           0 : static void dvb_encode_rle2(uint8_t **pq,
      41             :                             const uint8_t *bitmap, int linesize,
      42             :                             int w, int h)
      43             : {
      44             :     uint8_t *q;
      45             :     unsigned int bitbuf;
      46             :     int bitcnt;
      47             :     int x, y, len, x1, v, color;
      48             : 
      49           0 :     q = *pq;
      50             : 
      51           0 :     for(y = 0; y < h; y++) {
      52           0 :         *q++ = 0x10;
      53           0 :         bitbuf = 0;
      54           0 :         bitcnt = 6;
      55             : 
      56           0 :         x = 0;
      57           0 :         while (x < w) {
      58           0 :             x1 = x;
      59           0 :             color = bitmap[x1++];
      60           0 :             while (x1 < w && bitmap[x1] == color)
      61           0 :                 x1++;
      62           0 :             len = x1 - x;
      63           0 :             if (color == 0 && len == 2) {
      64           0 :                 PUTBITS2(0);
      65           0 :                 PUTBITS2(0);
      66           0 :                 PUTBITS2(1);
      67           0 :             } else if (len >= 3 && len <= 10) {
      68           0 :                 v = len - 3;
      69           0 :                 PUTBITS2(0);
      70           0 :                 PUTBITS2((v >> 2) | 2);
      71           0 :                 PUTBITS2(v & 3);
      72           0 :                 PUTBITS2(color);
      73           0 :             } else if (len >= 12 && len <= 27) {
      74           0 :                 v = len - 12;
      75           0 :                 PUTBITS2(0);
      76           0 :                 PUTBITS2(0);
      77           0 :                 PUTBITS2(2);
      78           0 :                 PUTBITS2(v >> 2);
      79           0 :                 PUTBITS2(v & 3);
      80           0 :                 PUTBITS2(color);
      81           0 :             } else if (len >= 29) {
      82             :                 /* length = 29 ... 284 */
      83           0 :                 if (len > 284)
      84           0 :                     len = 284;
      85           0 :                 v = len - 29;
      86           0 :                 PUTBITS2(0);
      87           0 :                 PUTBITS2(0);
      88           0 :                 PUTBITS2(3);
      89           0 :                 PUTBITS2((v >> 6));
      90           0 :                 PUTBITS2((v >> 4) & 3);
      91           0 :                 PUTBITS2((v >> 2) & 3);
      92           0 :                 PUTBITS2(v & 3);
      93           0 :                 PUTBITS2(color);
      94             :             } else {
      95           0 :                 PUTBITS2(color);
      96           0 :                 if (color == 0) {
      97           0 :                     PUTBITS2(1);
      98             :                 }
      99           0 :                 len = 1;
     100             :             }
     101           0 :             x += len;
     102             :         }
     103             :         /* end of line */
     104           0 :         PUTBITS2(0);
     105           0 :         PUTBITS2(0);
     106           0 :         PUTBITS2(0);
     107           0 :         if (bitcnt != 6) {
     108           0 :             *q++ = bitbuf;
     109             :         }
     110           0 :         *q++ = 0xf0;
     111           0 :         bitmap += linesize;
     112             :     }
     113           0 :     *pq = q;
     114           0 : }
     115             : 
     116             : #define PUTBITS4(val)\
     117             : {\
     118             :     bitbuf |= (val) << bitcnt;\
     119             :     bitcnt -= 4;\
     120             :     if (bitcnt < 0) {\
     121             :         bitcnt = 4;\
     122             :         *q++ = bitbuf;\
     123             :         bitbuf = 0;\
     124             :     }\
     125             : }
     126             : 
     127             : /* some DVB decoders only implement 4 bits/pixel */
     128           0 : static void dvb_encode_rle4(uint8_t **pq,
     129             :                             const uint8_t *bitmap, int linesize,
     130             :                             int w, int h)
     131             : {
     132             :     uint8_t *q;
     133             :     unsigned int bitbuf;
     134             :     int bitcnt;
     135             :     int x, y, len, x1, v, color;
     136             : 
     137           0 :     q = *pq;
     138             : 
     139           0 :     for(y = 0; y < h; y++) {
     140           0 :         *q++ = 0x11;
     141           0 :         bitbuf = 0;
     142           0 :         bitcnt = 4;
     143             : 
     144           0 :         x = 0;
     145           0 :         while (x < w) {
     146           0 :             x1 = x;
     147           0 :             color = bitmap[x1++];
     148           0 :             while (x1 < w && bitmap[x1] == color)
     149           0 :                 x1++;
     150           0 :             len = x1 - x;
     151           0 :             if (color == 0 && len == 2) {
     152           0 :                 PUTBITS4(0);
     153           0 :                 PUTBITS4(0xd);
     154           0 :             } else if (color == 0 && (len >= 3 && len <= 9)) {
     155           0 :                 PUTBITS4(0);
     156           0 :                 PUTBITS4(len - 2);
     157           0 :             } else if (len >= 4 && len <= 7) {
     158           0 :                 PUTBITS4(0);
     159           0 :                 PUTBITS4(8 + len - 4);
     160           0 :                 PUTBITS4(color);
     161           0 :             } else if (len >= 9 && len <= 24) {
     162           0 :                 PUTBITS4(0);
     163           0 :                 PUTBITS4(0xe);
     164           0 :                 PUTBITS4(len - 9);
     165           0 :                 PUTBITS4(color);
     166           0 :             } else if (len >= 25) {
     167           0 :                 if (len > 280)
     168           0 :                     len = 280;
     169           0 :                 v = len - 25;
     170           0 :                 PUTBITS4(0);
     171           0 :                 PUTBITS4(0xf);
     172           0 :                 PUTBITS4(v >> 4);
     173           0 :                 PUTBITS4(v & 0xf);
     174           0 :                 PUTBITS4(color);
     175             :             } else {
     176           0 :                 PUTBITS4(color);
     177           0 :                 if (color == 0) {
     178           0 :                     PUTBITS4(0xc);
     179             :                 }
     180           0 :                 len = 1;
     181             :             }
     182           0 :             x += len;
     183             :         }
     184             :         /* end of line */
     185           0 :         PUTBITS4(0);
     186           0 :         PUTBITS4(0);
     187           0 :         if (bitcnt != 4) {
     188           0 :             *q++ = bitbuf;
     189             :         }
     190           0 :         *q++ = 0xf0;
     191           0 :         bitmap += linesize;
     192             :     }
     193           0 :     *pq = q;
     194           0 : }
     195             : 
     196           0 : static void dvb_encode_rle8(uint8_t **pq,
     197             :                             const uint8_t *bitmap, int linesize,
     198             :                             int w, int h)
     199             : {
     200             :     uint8_t *q;
     201             :     int x, y, len, x1, color;
     202             : 
     203           0 :     q = *pq;
     204             : 
     205           0 :     for (y = 0; y < h; y++) {
     206           0 :         *q++ = 0x12;
     207             : 
     208           0 :         x = 0;
     209           0 :         while (x < w) {
     210           0 :             x1 = x;
     211           0 :             color = bitmap[x1++];
     212           0 :             while (x1 < w && bitmap[x1] == color)
     213           0 :                 x1++;
     214           0 :             len = x1 - x;
     215           0 :             if (len == 1 && color) {
     216             :                 // 00000001 to 11111111           1 pixel in colour x
     217           0 :                 *q++ = color;
     218             :             } else {
     219           0 :                 if (color == 0x00) {
     220             :                     // 00000000 0LLLLLLL          L pixels (1-127) in colour 0 (L > 0)
     221           0 :                     len = FFMIN(len, 127);
     222           0 :                     *q++ = 0x00;
     223           0 :                     *q++ = len;
     224           0 :                 } else if (len > 2) {
     225             :                     // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
     226           0 :                     len = FFMIN(len, 127);
     227           0 :                     *q++ = 0x00;
     228           0 :                     *q++ = 0x80+len;
     229           0 :                     *q++ = color;
     230             :                 }
     231           0 :                 else if (len == 2) {
     232           0 :                     *q++ = color;
     233           0 :                     *q++ = color;
     234             :                 } else {
     235           0 :                     *q++ = color;
     236           0 :                     len = 1;
     237             :                 }
     238             :             }
     239           0 :             x += len;
     240             :         }
     241             :         /* end of line */
     242             :         // 00000000 end of 8-bit/pixel_code_string
     243           0 :         *q++ = 0x00;
     244           0 :         *q++ = 0xf0;
     245           0 :         bitmap += linesize;
     246             :     }
     247           0 :     *pq = q;
     248           0 : }
     249             : 
     250           0 : static int encode_dvb_subtitles(DVBSubtitleContext *s,
     251             :                                 uint8_t *outbuf, const AVSubtitle *h)
     252             : {
     253             :     uint8_t *q, *pseg_len;
     254             :     int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
     255             : 
     256             : 
     257           0 :     q = outbuf;
     258             : 
     259           0 :     page_id = 1;
     260             : 
     261           0 :     if (h->num_rects && !h->rects)
     262           0 :         return -1;
     263             : 
     264             :     /* page composition segment */
     265             : 
     266           0 :     *q++ = 0x0f; /* sync_byte */
     267           0 :     *q++ = 0x10; /* segment_type */
     268           0 :     bytestream_put_be16(&q, page_id);
     269           0 :     pseg_len = q;
     270           0 :     q += 2; /* segment length */
     271           0 :     *q++ = 30; /* page_timeout (seconds) */
     272           0 :     page_state = 2; /* mode change */
     273             :     /* page_version = 0 + page_state */
     274           0 :     *q++ = (s->object_version << 4) | (page_state << 2) | 3;
     275             : 
     276           0 :     for (region_id = 0; region_id < h->num_rects; region_id++) {
     277           0 :         *q++ = region_id;
     278           0 :         *q++ = 0xff; /* reserved */
     279           0 :         bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
     280           0 :         bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
     281             :     }
     282             : 
     283           0 :     bytestream_put_be16(&pseg_len, q - pseg_len - 2);
     284             : 
     285           0 :     if (h->num_rects) {
     286           0 :         for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
     287             : 
     288             :             /* CLUT segment */
     289             : 
     290           0 :             if (h->rects[clut_id]->nb_colors <= 4) {
     291             :                 /* 2 bpp, some decoders do not support it correctly */
     292           0 :                 bpp_index = 0;
     293           0 :             } else if (h->rects[clut_id]->nb_colors <= 16) {
     294             :                 /* 4 bpp, standard encoding */
     295           0 :                 bpp_index = 1;
     296           0 :             } else if (h->rects[clut_id]->nb_colors <= 256) {
     297             :                 /* 8 bpp, standard encoding */
     298           0 :                 bpp_index = 2;
     299             :             } else {
     300           0 :                 return -1;
     301             :             }
     302             : 
     303             : 
     304             :             /* CLUT segment */
     305           0 :             *q++ = 0x0f; /* sync byte */
     306           0 :             *q++ = 0x12; /* CLUT definition segment */
     307           0 :             bytestream_put_be16(&q, page_id);
     308           0 :             pseg_len = q;
     309           0 :             q += 2; /* segment length */
     310           0 :             *q++ = clut_id;
     311           0 :             *q++ = (0 << 4) | 0xf; /* version = 0 */
     312             : 
     313           0 :             for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
     314           0 :                 *q++ = i; /* clut_entry_id */
     315           0 :                 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
     316             :                 {
     317             :                     int a, r, g, b;
     318           0 :                     uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
     319           0 :                     a = (x >> 24) & 0xff;
     320           0 :                     r = (x >> 16) & 0xff;
     321           0 :                     g = (x >>  8) & 0xff;
     322           0 :                     b = (x >>  0) & 0xff;
     323             : 
     324           0 :                     *q++ = RGB_TO_Y_CCIR(r, g, b);
     325           0 :                     *q++ = RGB_TO_V_CCIR(r, g, b, 0);
     326           0 :                     *q++ = RGB_TO_U_CCIR(r, g, b, 0);
     327           0 :                     *q++ = 255 - a;
     328             :                 }
     329             :             }
     330             : 
     331           0 :             bytestream_put_be16(&pseg_len, q - pseg_len - 2);
     332             :         }
     333             :     }
     334             : 
     335           0 :     for (region_id = 0; region_id < h->num_rects; region_id++) {
     336             : 
     337             :         /* region composition segment */
     338             : 
     339           0 :         if (h->rects[region_id]->nb_colors <= 4) {
     340             :             /* 2 bpp, some decoders do not support it correctly */
     341           0 :             bpp_index = 0;
     342           0 :         } else if (h->rects[region_id]->nb_colors <= 16) {
     343             :             /* 4 bpp, standard encoding */
     344           0 :             bpp_index = 1;
     345           0 :         } else if (h->rects[region_id]->nb_colors <= 256) {
     346             :             /* 8 bpp, standard encoding */
     347           0 :             bpp_index = 2;
     348             :         } else {
     349           0 :             return -1;
     350             :         }
     351             : 
     352           0 :         *q++ = 0x0f; /* sync_byte */
     353           0 :         *q++ = 0x11; /* segment_type */
     354           0 :         bytestream_put_be16(&q, page_id);
     355           0 :         pseg_len = q;
     356           0 :         q += 2; /* segment length */
     357           0 :         *q++ = region_id;
     358           0 :         *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
     359           0 :         bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
     360           0 :         bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
     361           0 :         *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
     362           0 :         *q++ = region_id; /* clut_id == region_id */
     363           0 :         *q++ = 0; /* 8 bit fill colors */
     364           0 :         *q++ = 0x03; /* 4 bit and 2 bit fill colors */
     365             : 
     366           0 :         bytestream_put_be16(&q, region_id); /* object_id == region_id */
     367           0 :         *q++ = (0 << 6) | (0 << 4);
     368           0 :         *q++ = 0;
     369           0 :         *q++ = 0xf0;
     370           0 :         *q++ = 0;
     371             : 
     372           0 :         bytestream_put_be16(&pseg_len, q - pseg_len - 2);
     373             :     }
     374             : 
     375           0 :     if (h->num_rects) {
     376             : 
     377           0 :         for (object_id = 0; object_id < h->num_rects; object_id++) {
     378             :             void (*dvb_encode_rle)(uint8_t **pq,
     379             :                                     const uint8_t *bitmap, int linesize,
     380             :                                     int w, int h);
     381             : 
     382             :             /* bpp_index maths */
     383           0 :             if (h->rects[object_id]->nb_colors <= 4) {
     384             :                 /* 2 bpp, some decoders do not support it correctly */
     385           0 :                 dvb_encode_rle = dvb_encode_rle2;
     386           0 :             } else if (h->rects[object_id]->nb_colors <= 16) {
     387             :                 /* 4 bpp, standard encoding */
     388           0 :                 dvb_encode_rle = dvb_encode_rle4;
     389           0 :             } else if (h->rects[object_id]->nb_colors <= 256) {
     390             :                 /* 8 bpp, standard encoding */
     391           0 :                 dvb_encode_rle = dvb_encode_rle8;
     392             :             } else {
     393           0 :                 return -1;
     394             :             }
     395             : 
     396             :             /* Object Data segment */
     397           0 :             *q++ = 0x0f; /* sync byte */
     398           0 :             *q++ = 0x13;
     399           0 :             bytestream_put_be16(&q, page_id);
     400           0 :             pseg_len = q;
     401           0 :             q += 2; /* segment length */
     402             : 
     403           0 :             bytestream_put_be16(&q, object_id);
     404           0 :             *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
     405             :                                                                        onject_coding_method,
     406             :                                                                        non_modifying_color_flag */
     407             :             {
     408             :                 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
     409             : 
     410           0 :                 ptop_field_len = q;
     411           0 :                 q += 2;
     412           0 :                 pbottom_field_len = q;
     413           0 :                 q += 2;
     414             : 
     415           0 :                 top_ptr = q;
     416           0 :                 dvb_encode_rle(&q, h->rects[object_id]->data[0], h->rects[object_id]->w * 2,
     417           0 :                                     h->rects[object_id]->w, h->rects[object_id]->h >> 1);
     418           0 :                 bottom_ptr = q;
     419           0 :                 dvb_encode_rle(&q, h->rects[object_id]->data[0] + h->rects[object_id]->w,
     420           0 :                                     h->rects[object_id]->w * 2, h->rects[object_id]->w,
     421           0 :                                     h->rects[object_id]->h >> 1);
     422             : 
     423           0 :                 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
     424           0 :                 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
     425             :             }
     426             : 
     427           0 :             bytestream_put_be16(&pseg_len, q - pseg_len - 2);
     428             :         }
     429             :     }
     430             : 
     431             :     /* end of display set segment */
     432             : 
     433           0 :     *q++ = 0x0f; /* sync_byte */
     434           0 :     *q++ = 0x80; /* segment_type */
     435           0 :     bytestream_put_be16(&q, page_id);
     436           0 :     pseg_len = q;
     437           0 :     q += 2; /* segment length */
     438             : 
     439           0 :     bytestream_put_be16(&pseg_len, q - pseg_len - 2);
     440             : 
     441           0 :     s->object_version = (s->object_version + 1) & 0xf;
     442           0 :     return q - outbuf;
     443             : }
     444             : 
     445           0 : static int dvbsub_encode(AVCodecContext *avctx,
     446             :                          unsigned char *buf, int buf_size,
     447             :                          const AVSubtitle *sub)
     448             : {
     449           0 :     DVBSubtitleContext *s = avctx->priv_data;
     450             :     int ret;
     451             : 
     452           0 :     ret = encode_dvb_subtitles(s, buf, sub);
     453           0 :     return ret;
     454             : }
     455             : 
     456             : AVCodec ff_dvbsub_encoder = {
     457             :     .name           = "dvbsub",
     458             :     .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
     459             :     .type           = AVMEDIA_TYPE_SUBTITLE,
     460             :     .id             = AV_CODEC_ID_DVB_SUBTITLE,
     461             :     .priv_data_size = sizeof(DVBSubtitleContext),
     462             :     .encode_sub     = dvbsub_encode,
     463             : };

Generated by: LCOV version 1.13