Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * VC-1 and WMV3 parser
  3.  * Copyright (c) 2006-2007 Konstantin Shishkov
  4.  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  5.  *
  6.  * This file is part of FFmpeg.
  7.  *
  8.  * FFmpeg is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * FFmpeg is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with FFmpeg; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  */
  22.  
  23. /**
  24.  * @file
  25.  * VC-1 and WMV3 parser
  26.  */
  27.  
  28. #include "libavutil/attributes.h"
  29. #include "parser.h"
  30. #include "vc1.h"
  31. #include "get_bits.h"
  32. #include "internal.h"
  33.  
  34. /** The maximum number of bytes of a sequence, entry point or
  35.  *  frame header whose values we pay any attention to */
  36. #define UNESCAPED_THRESHOLD 37
  37.  
  38. /** The maximum number of bytes of a sequence, entry point or
  39.  *  frame header which must be valid memory (because they are
  40.  *  used to update the bitstream cache in skip_bits() calls)
  41.  */
  42. #define UNESCAPED_LIMIT 144
  43.  
  44. typedef enum {
  45.     NO_MATCH,
  46.     ONE_ZERO,
  47.     TWO_ZEROS,
  48.     ONE
  49. } VC1ParseSearchState;
  50.  
  51. typedef struct VC1ParseContext {
  52.     ParseContext pc;
  53.     VC1Context v;
  54.     uint8_t prev_start_code;
  55.     size_t bytes_to_skip;
  56.     uint8_t unesc_buffer[UNESCAPED_LIMIT];
  57.     size_t unesc_index;
  58.     VC1ParseSearchState search_state;
  59. } VC1ParseContext;
  60.  
  61. static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
  62.                                const uint8_t *buf, int buf_size)
  63. {
  64.     /* Parse the header we just finished unescaping */
  65.     VC1ParseContext *vpc = s->priv_data;
  66.     GetBitContext gb;
  67.     int ret;
  68.     vpc->v.s.avctx = avctx;
  69.     vpc->v.parse_only = 1;
  70.     init_get_bits(&gb, buf, buf_size * 8);
  71.     switch (vpc->prev_start_code) {
  72.     case VC1_CODE_SEQHDR & 0xFF:
  73.         ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
  74.         break;
  75.     case VC1_CODE_ENTRYPOINT & 0xFF:
  76.         ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
  77.         break;
  78.     case VC1_CODE_FRAME & 0xFF:
  79.         if(vpc->v.profile < PROFILE_ADVANCED)
  80.             ret = ff_vc1_parse_frame_header    (&vpc->v, &gb);
  81.         else
  82.             ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
  83.  
  84.         if (ret < 0)
  85.             break;
  86.  
  87.         /* keep AV_PICTURE_TYPE_BI internal to VC1 */
  88.         if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
  89.             s->pict_type = AV_PICTURE_TYPE_B;
  90.         else
  91.             s->pict_type = vpc->v.s.pict_type;
  92.  
  93.         if (avctx->ticks_per_frame > 1){
  94.             // process pulldown flags
  95.             s->repeat_pict = 1;
  96.             // Pulldown flags are only valid when 'broadcast' has been set.
  97.             // So ticks_per_frame will be 2
  98.             if (vpc->v.rff){
  99.                 // repeat field
  100.                 s->repeat_pict = 2;
  101.             }else if (vpc->v.rptfrm){
  102.                 // repeat frames
  103.                 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
  104.             }
  105.         }else{
  106.             s->repeat_pict = 0;
  107.         }
  108.  
  109.         if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
  110.             s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
  111.         else
  112.             s->field_order = AV_FIELD_PROGRESSIVE;
  113.  
  114.         break;
  115.     }
  116.     if (avctx->framerate.num)
  117.         avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  118. }
  119.  
  120. static int vc1_parse(AVCodecParserContext *s,
  121.                            AVCodecContext *avctx,
  122.                            const uint8_t **poutbuf, int *poutbuf_size,
  123.                            const uint8_t *buf, int buf_size)
  124. {
  125.     /* Here we do the searching for frame boundaries and headers at
  126.      * the same time. Only a minimal amount at the start of each
  127.      * header is unescaped. */
  128.     VC1ParseContext *vpc = s->priv_data;
  129.     int pic_found = vpc->pc.frame_start_found;
  130.     uint8_t *unesc_buffer = vpc->unesc_buffer;
  131.     size_t unesc_index = vpc->unesc_index;
  132.     VC1ParseSearchState search_state = vpc->search_state;
  133.     int start_code_found = 0;
  134.     int next = END_NOT_FOUND;
  135.     int i = vpc->bytes_to_skip;
  136.  
  137.     if (pic_found && buf_size == 0) {
  138.         /* EOF considered as end of frame */
  139.         memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
  140.         vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  141.         next = 0;
  142.     }
  143.     while (i < buf_size) {
  144.         uint8_t b;
  145.         start_code_found = 0;
  146.         while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
  147.             b = buf[i++];
  148.             unesc_buffer[unesc_index++] = b;
  149.             if (search_state <= ONE_ZERO)
  150.                 search_state = b ? NO_MATCH : search_state + 1;
  151.             else if (search_state == TWO_ZEROS) {
  152.                 if (b == 1)
  153.                     search_state = ONE;
  154.                 else if (b > 1) {
  155.                     if (b == 3)
  156.                         unesc_index--; // swallow emulation prevention byte
  157.                     search_state = NO_MATCH;
  158.                 }
  159.             }
  160.             else { // search_state == ONE
  161.                 // Header unescaping terminates early due to detection of next start code
  162.                 search_state = NO_MATCH;
  163.                 start_code_found = 1;
  164.                 break;
  165.             }
  166.         }
  167.         if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
  168.                 unesc_index >= UNESCAPED_THRESHOLD &&
  169.                 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
  170.         {
  171.             // No need to keep scanning the rest of the buffer for
  172.             // start codes if we know it contains a complete frame and
  173.             // we've already unescaped all we need of the frame header
  174.             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  175.             break;
  176.         }
  177.         if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
  178.             while (i < buf_size) {
  179.                 if (search_state == NO_MATCH) {
  180.                     i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
  181.                     if (i < buf_size) {
  182.                         search_state = ONE_ZERO;
  183.                     }
  184.                     i++;
  185.                 } else {
  186.                     b = buf[i++];
  187.                     if (search_state == ONE_ZERO)
  188.                         search_state = b ? NO_MATCH : TWO_ZEROS;
  189.                     else if (search_state == TWO_ZEROS) {
  190.                         if (b >= 1)
  191.                             search_state = b == 1 ? ONE : NO_MATCH;
  192.                     }
  193.                     else { // search_state == ONE
  194.                         search_state = NO_MATCH;
  195.                         start_code_found = 1;
  196.                         break;
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.         if (start_code_found) {
  202.             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  203.  
  204.             vpc->prev_start_code = b;
  205.             unesc_index = 0;
  206.  
  207.             if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
  208.                 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
  209.                     pic_found = 1;
  210.                 }
  211.                 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
  212.                     next = i - 4;
  213.                     pic_found = b == (VC1_CODE_FRAME & 0xFF);
  214.                     break;
  215.                 }
  216.             }
  217.         }
  218.     }
  219.  
  220.     vpc->pc.frame_start_found = pic_found;
  221.     vpc->unesc_index = unesc_index;
  222.     vpc->search_state = search_state;
  223.  
  224.     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  225.         next = buf_size;
  226.     } else {
  227.         if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  228.             vpc->bytes_to_skip = 0;
  229.             *poutbuf = NULL;
  230.             *poutbuf_size = 0;
  231.             return buf_size;
  232.         }
  233.     }
  234.  
  235.     /* If we return with a valid pointer to a combined frame buffer
  236.      * then on the next call then we'll have been unhelpfully rewound
  237.      * by up to 4 bytes (depending upon whether the start code
  238.      * overlapped the input buffer, and if so by how much). We don't
  239.      * want this: it will either cause spurious second detections of
  240.      * the start code we've already seen, or cause extra bytes to be
  241.      * inserted at the start of the unescaped buffer. */
  242.     vpc->bytes_to_skip = 4;
  243.     if (next < 0 && next != END_NOT_FOUND)
  244.         vpc->bytes_to_skip += next;
  245.  
  246.     *poutbuf = buf;
  247.     *poutbuf_size = buf_size;
  248.     return next;
  249. }
  250.  
  251. static int vc1_split(AVCodecContext *avctx,
  252.                            const uint8_t *buf, int buf_size)
  253. {
  254.     uint32_t state = -1;
  255.     int charged = 0;
  256.     const uint8_t *ptr = buf, *end = buf + buf_size;
  257.  
  258.     while (ptr < end) {
  259.         ptr = avpriv_find_start_code(ptr, end, &state);
  260.         if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  261.             charged = 1;
  262.         } else if (charged && IS_MARKER(state))
  263.             return ptr - 4 - buf;
  264.     }
  265.  
  266.     return 0;
  267. }
  268.  
  269. static av_cold int vc1_parse_init(AVCodecParserContext *s)
  270. {
  271.     VC1ParseContext *vpc = s->priv_data;
  272.     vpc->v.s.slice_context_count = 1;
  273.     vpc->v.first_pic_header_flag = 1;
  274.     vpc->prev_start_code = 0;
  275.     vpc->bytes_to_skip = 0;
  276.     vpc->unesc_index = 0;
  277.     vpc->search_state = NO_MATCH;
  278.     return ff_vc1_init_common(&vpc->v);
  279. }
  280.  
  281. AVCodecParser ff_vc1_parser = {
  282.     .codec_ids      = { AV_CODEC_ID_VC1 },
  283.     .priv_data_size = sizeof(VC1ParseContext),
  284.     .parser_init    = vc1_parse_init,
  285.     .parser_parse   = vc1_parse,
  286.     .parser_close   = ff_parse_close,
  287.     .split          = vc1_split,
  288. };
  289.