Ticket #11932: mythtv-h264cc.patch

File mythtv-h264cc.patch, 14.5 KB (added by anonymous, 10 years ago)
  • mythtv/external/FFmpeg/libavcodec/h264.c

    diff --git a/mythtv/external/FFmpeg/libavcodec/h264.c b/mythtv/external/FFmpeg/libavcodec/h264.c
    index 3a83b4b..e8c0735 100644
    a b  
    17421742
    17431743    pic->f.reference            = h->droppable ? 0 : h->picture_structure;
    17441744    pic->f.coded_picture_number = h->coded_picture_number++;
    17451745    pic->field_picture          = h->picture_structure != PICT_FRAME;
    17461746
     1747
     1748    /* Put ATSC captions cached from parse_user_data into the correct frame */
     1749    memcpy(pic->f.atsc_cc_buf, h->tmp_atsc_cc_buf, h->tmp_atsc_cc_len);
     1750    pic->f.atsc_cc_len = h->tmp_atsc_cc_len;
     1751    h->tmp_atsc_cc_len = 0;
     1752    memcpy(pic->f.scte_cc_buf, h->tmp_scte_cc_buf, h->tmp_scte_cc_len);
     1753    pic->f.scte_cc_len = h->tmp_scte_cc_len;
     1754    h->tmp_scte_cc_len = 0;
     1755
    17471756    /*
    17481757     * Zero key_frame here; IDR markings per slice in frame or fields are ORed
    17491758     * in later.
    17501759     * See decode_nal_units().
    17511760     */
     
    27752784     * and a bad error table. Further, the error count goes to
    27762785     * INT_MAX when called for bottom field, because mb_y is
    27772786     * past end by one (callers fault) and resync_mb_y != 0
    27782787     * causes problems for the first MB line, too.
    27792788     */
    2780     if (CONFIG_ERROR_RESILIENCE &&
    2781         !FIELD_PICTURE && h->current_slice && !h->sps.new) {
     2789    if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE && h->current_slice && !h->sps.new && h->avctx->skip_frame < AVDISCARD_ALL) {
    27822790        h->er.cur_pic  = h->cur_pic_ptr;
    27832791        ff_er_frame_end(&h->er);
    27842792    }
    27852793    emms_c();
    27862794
     
    45194527                case NAL_SLICE:
    45204528                    first_slice = hx->nal_unit_type;
    45214529                }
    45224530
    45234531            // FIXME do not discard SEI id
    4524             if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
    4525                 continue;
     4532            if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0
     4533                        && hx->nal_unit_type != NAL_SLICE && hx->nal_unit_type != NAL_SEI)
     4534                continue;
    45264535
    45274536again:
    45284537            /* Ignore per frame NAL unit type during extradata
    45294538             * parsing. Decoding slices is not possible in codec init
    45304539             * with frame-mt */
  • mythtv/external/FFmpeg/libavcodec/h264.h

    diff --git a/mythtv/external/FFmpeg/libavcodec/h264.h b/mythtv/external/FFmpeg/libavcodec/h264.h
    index 03be472..03b4a1c 100644
    a b  
    638638
    639639    int16_t slice_row[MAX_SLICES]; ///< to detect when MAX_SLICES is too low
    640640
    641641    int sync;                      ///< did we had a keyframe or recovery point
    642642
     643#define ATSC_CC_BUF_SIZE 1024
     644    /// Used to hold cached user_data about caption packets before the
     645    /// frame for these packets has been created in MPV_frame_start().
     646    uint8_t tmp_atsc_cc_buf[ATSC_CC_BUF_SIZE];
     647    int     tmp_atsc_cc_len;
     648#define SCTE_CC_BUF_SIZE 1024
     649    uint8_t tmp_scte_cc_buf[SCTE_CC_BUF_SIZE];
     650    int     tmp_scte_cc_len;
     651
    643652    uint8_t parse_history[4];
    644653    int parse_history_count;
    645654    int parse_last_mb;
    646655    uint8_t *edge_emu_buffer;
    647656    int16_t *dc_val_base;
  • mythtv/external/FFmpeg/libavcodec/h264_sei.c

    diff --git a/mythtv/external/FFmpeg/libavcodec/h264_sei.c b/mythtv/external/FFmpeg/libavcodec/h264_sei.c
    index ece54f1..adae78b 100644
    a b  
    9292            av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", h->sei_ct_type, h->sei_pic_struct);
    9393    }
    9494    return 0;
    9595}
    9696
    97 static int decode_user_data_itu_t_t35(H264Context *h, int size) {
    98     uint32_t user_identifier;
    99     int dtg_active_format;
     97static int decode_user_data_itu_t_t35(H264Context *s, int buf_size) {
    10098
    101     if (size < 7)
    102         return -1;
    103     size -= 7;
     99    const uint8_t *p = NULL, *buf_end = NULL;
    104100
    105     skip_bits(&h->gb, 8);   // country_code
    106     skip_bits(&h->gb, 16);  // provider_code
    107     user_identifier = get_bits_long(&h->gb, 32);
     101    //Jump index bytes to start of user data
     102    //Starts with 0xB5 and 0x0031
     103    p = s->gb.buffer + (s->gb.index >> 3);
    108104
    109     switch (user_identifier) {
    110         case 0x44544731:    // "DTG1" - AFD_data
    111             if (size < 1)
    112                 return -1;
    113             skip_bits(&h->gb, 1);
    114             if (get_bits(&h->gb, 1)) {
    115                 skip_bits(&h->gb, 6);
    116                 if (size < 2)
    117                     return -1;
    118                 skip_bits(&h->gb, 4);
    119                 dtg_active_format = get_bits(&h->gb, 4);
    120                 h->avctx->dtg_active_format = dtg_active_format;
    121             } else {
    122                 skip_bits(&h->gb, 6);
     105    /*Ignore first 3 bytes
     106    p[0] = 0xB5 ITU-T Country Code
     107    p[1-2] = 0x0031 ITU-T Provider code */
     108    p += 3; //ignore 0xB50031
     109    buf_end = p + buf_size-3;
     110
     111    /*we parse the DTG active format information */
     112    if (buf_end - p >= 5 &&
     113            p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
     114        int flags = p[4];
     115        p += 5;
     116        if (flags & 0x80) { //skip event id
     117            p += 2;
     118        }
     119        if (flags & 0x40) {
     120            if (buf_end - p < 1)
     121                return;
     122            s->avctx->dtg_active_format = p[0] & 0x0f;
     123        }
     124    } else if (buf_end - p >= 6 &&
     125            p[0] == 0x43 && p[1] == 0x43 && p[2] == 0x01 && p[3] == 0xf8 &&
     126            p[4] == 0x9e) {
     127#undef fprintf
     128        int atsc_cnt_loc = s->tmp_atsc_cc_len;
     129        uint8_t real_count = 0;
     130        unsigned int i;
     131
     132        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&real_count);
     133        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data
     134
     135        for (i=5; i < (buf_end - p - 2) &&
     136        (s->tmp_atsc_cc_len + 3) < ATSC_CC_BUF_SIZE; i++)
     137        {
     138            if ((p[i]&0xfe) == 0xfe) // CC1&CC2 || CC3&CC4
     139            {
     140                uint8_t type = (p[i] & 0x01) ^ 0x01;
     141                uint8_t cc_data_1 = p[++i];
     142                uint8_t cc_data_2 = p[++i];
     143                uint8_t valid = 1;
     144                uint8_t cc608_hdr = 0xf8 | (valid ? 0x04 : 0x00) | type;
     145                real_count++;
     146                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr;
     147                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc_data_1;
     148                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc_data_2;
     149                continue;
    123150            }
    124151            break;
    125         default:
    126             skip_bits(&h->gb, size * 8);
    127             break;
    128     }
     152        }
     153        if (!real_count)
     154        {
     155            s->tmp_atsc_cc_len = atsc_cnt_loc;
     156        }
     157        else
     158        {
     159            s->tmp_atsc_cc_buf[atsc_cnt_loc] = 0x40 | (0x1f&real_count);
     160            s->tmp_atsc_cc_len = atsc_cnt_loc + 2 + 3 * real_count;
     161        }
     162    } else if (buf_end - p >= 6 &&
     163            p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4') {
     164        /* Parse CEA-708/608 Closed Captions in ATSC user data */
     165        int user_data_type_code = p[4];
     166        if (user_data_type_code == 0x03) { // caption data
     167            int cccnt = p[5] & 0x1f;
     168            int cclen = 3 * cccnt + 2;
     169            int proc = (p[5] >> 6) & 1;
     170            int blen = s->tmp_atsc_cc_len;
    129171
     172            p += 5;
     173
     174            if ((cclen <= buf_end - p) && ((cclen + blen) < ATSC_CC_BUF_SIZE)) {
     175                uint8_t *dst = s->tmp_atsc_cc_buf + s->tmp_atsc_cc_len;
     176                memcpy(dst, p, cclen);
     177                s->tmp_atsc_cc_len += cclen;
     178            }
     179        }
     180        else if (user_data_type_code == 0x04) {
     181            // additional CEA-608 data, as per SCTE 21
     182        }
     183        else if (user_data_type_code == 0x05) {
     184            // luma PAM data, as per SCTE 21
     185        }
     186        else if (user_data_type_code == 0x06) {
     187            // bar data (letterboxing info)
     188        }
     189    } else if (buf_end - p >= 3 && p[0] == 0x03 && ((p[1]&0x7f) == 0x01)) {
     190        // SCTE 20 encoding of CEA-608
     191        unsigned int cc_count = p[2]>>3;
     192        unsigned int cc_bits = cc_count * 26;
     193        unsigned int cc_bytes = (cc_bits + 7 - 3) / 8;
     194        if (buf_end - p >= (2+cc_bytes) && (s->tmp_scte_cc_len + 2 + 3*cc_count) < SCTE_CC_BUF_SIZE) {
     195            int scte_cnt_loc = s->tmp_scte_cc_len;
     196            uint8_t real_count = 0, marker = 1, i;
     197            GetBitContext gb;
     198            init_get_bits(&gb, p+2, (buf_end-p-2) * sizeof(uint8_t));
     199            get_bits(&gb, 5); // swallow cc_count
     200            s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = 0x40 | (0x1f&cc_count);
     201            s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = 0x00; // em_data
     202            for (i = 0; i < cc_count; i++) {
     203                uint8_t valid, cc608_hdr;
     204                uint8_t priority = get_bits(&gb, 2);
     205                uint8_t field_no = get_bits(&gb, 2);
     206                uint8_t line_offset = get_bits(&gb, 5);
     207                uint8_t cc_data_1 = av_reverse[get_bits(&gb, 8)];
     208                uint8_t cc_data_2 = av_reverse[get_bits(&gb, 8)];
     209                uint8_t type = (1 == field_no) ? 0x00 : 0x01;
     210                (void) priority; // we use all the data, don't need priority
     211                marker &= get_bits(&gb, 1);
     212                // dump if marker bit missing
     213                valid = marker;
     214                // ignore forbidden and repeated (3:2 pulldown) field numbers
     215                valid = valid && (1 == field_no || 2 == field_no);
     216                // ignore content not in line 21
     217                valid = valid && (11 == line_offset);
     218                if (!valid)
     219                    continue;
     220                cc608_hdr = 0xf8 | (valid ? 0x04 : 0x00) | type;
     221                real_count++;
     222                s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc608_hdr;
     223                s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc_data_1;
     224                s->tmp_scte_cc_buf[s->tmp_scte_cc_len++] = cc_data_2;
     225            }
     226            if (!real_count)
     227            {
     228                s->tmp_scte_cc_len = scte_cnt_loc;
     229            }
     230            else
     231            {
     232                s->tmp_scte_cc_buf[scte_cnt_loc] = 0x40 | (0x1f&real_count);
     233                s->tmp_scte_cc_len = scte_cnt_loc + 2 + 3 * real_count;
     234            }
     235        }
     236    } else if (buf_end - p >= 11 &&
     237            p[0] == 0x05 && p[1] == 0x02) {
     238        /* parse EIA-608 captions embedded in a DVB stream. */
     239        uint8_t dvb_cc_type = p[7];
     240        p += 8;
     241
     242        /* Predictive frame tag, but MythTV reorders predictive
     243         * frames for us along with the CC data, so we ignore it.
     244         */
     245        if (dvb_cc_type == 0x05) {
     246            dvb_cc_type = p[6];
     247            p += 7;
     248        }
     249
     250        if (dvb_cc_type == 0x02) { /* 2-byte caption, can be repeated */
     251            int type = 0x00; // line 21 field 1 == 0x00, field 2 == 0x01
     252            uint8_t cc608_hdr = 0xf8 | 0x04/*valid*/ | type;
     253            uint8_t hi = p[1] & 0xFF;
     254            uint8_t lo = p[2] & 0xFF;
     255
     256            dvb_cc_type = p[3];
     257
     258            if ((2 <= buf_end - p) && ((3 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) {
     259                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&1/*cc_count*/);
     260                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data
     261                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr;
     262                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = hi;
     263                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = lo;
     264
     265                /* Only repeat characters when the next type flag
     266                 * is 0x04 and the characters are repeatable (i.e., less than
     267                 * 32 with the parity stripped).
     268                 */
     269                if (dvb_cc_type == 0x04 && (hi & 0x7f) < 32) {
     270                    if ((2 <= buf_end - p) && ((3 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) {
     271                        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&1/*cc_count*/);
     272                        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data
     273                        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr;
     274                        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = hi;
     275                        s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = lo;
     276                    }
     277                }
     278            }
     279
     280            p += 6;
     281        } else if (dvb_cc_type == 0x04) { /* 4-byte caption, not repeated */
     282            if ((4 <= buf_end - p) &&
     283                    ((6 + s->tmp_atsc_cc_len) < ATSC_CC_BUF_SIZE)) {
     284                int type = 0x00; // line 21 field 1 == 0x00, field 2 == 0x01
     285                uint8_t cc608_hdr = 0xf8 | 0x04/*valid*/ | type;
     286
     287                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x40 | (0x1f&2/*cc_count*/);
     288                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = 0x00; // em_data
     289                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr;
     290                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[1] & 0xFF;
     291                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[2] & 0xFF;
     292                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = cc608_hdr;
     293                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[3] & 0xFF;
     294                s->tmp_atsc_cc_buf[s->tmp_atsc_cc_len++] = p[4] & 0xFF;
     295            }
     296
     297            p += 9;
     298        }
     299    }
     300    // For other CEA-608 embedding options see:
     301    /* SCTE 21 */
     302    /* ETSI EN 301 775 */
    130303    return 0;
    131304}
    132305
    133306static int decode_unregistered_user_data(H264Context *h, int size){
    134307    uint8_t user_data[16+256];
     
    221394                return -1;
    222395            break;
    223396        case SEI_TYPE_USER_DATA_ITU_T_T35:
    224397            if(decode_user_data_itu_t_t35(h, size) < 0)
    225398                return -1;
     399            skip_bits(&h->gb, 8*size);
    226400            break;
    227401        case SEI_TYPE_USER_DATA_UNREGISTERED:
    228402            if(decode_unregistered_user_data(h, size) < 0)
    229403                return -1;
    230404            break;
  • mythtv/external/FFmpeg/libavutil/common.h

    diff --git a/mythtv/external/FFmpeg/libavutil/common.h b/mythtv/external/FFmpeg/libavutil/common.h
    index beaf9f7..0427466 100644
    a b  
    3232#include <math.h>
    3333#include <stdio.h>
    3434#include <stdlib.h>
    3535#include <string.h>
    3636
     37#ifndef UINT64_C
     38#define UINT64_C(c) (c ## ULL)
     39#endif
     40
     41
    3742#include "attributes.h"
    3843#include "version.h"
    3944#include "libavutil/avconfig.h"
    4045
    4146#if AV_HAVE_BIGENDIAN
  • mythtv/libs/libmythtv/avformatdecoder.cpp

    diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
    index 3a42b40..bc425e4 100644
    a b  
    14331433        }
    14341434
    14351435        if (FlagIsSet(kDecodeNoDecode))
    14361436        {
    14371437            enc->skip_idct = AVDISCARD_ALL;
     1438            enc->skip_frame = AVDISCARD_ALL;
    14381439        }
    14391440    }
    14401441
    14411442    if (selectedStream)
    14421443    {