Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Dirac parser
  3.  *
  4.  * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
  5.  * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
  6.  *
  7.  * This file is part of FFmpeg.
  8.  *
  9.  * FFmpeg is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  *
  14.  * FFmpeg is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with FFmpeg; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22.  */
  23.  
  24. /**
  25.  * @file
  26.  * Dirac Parser
  27.  * @author Marco Gerards <marco@gnu.org>
  28.  */
  29.  
  30. #include <string.h>
  31.  
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/mem.h"
  34.  
  35. #include "parser.h"
  36.  
  37. #define DIRAC_PARSE_INFO_PREFIX 0x42424344
  38.  
  39. /**
  40.  * Find the end of the current frame in the bitstream.
  41.  * @return the position of the first byte of the next frame or -1
  42.  */
  43. typedef struct DiracParseContext {
  44.     int state;
  45.     int is_synced;
  46.     int sync_offset;
  47.     int header_bytes_needed;
  48.     int overread_index;
  49.     int buffer_size;
  50.     int index;
  51.     uint8_t *buffer;
  52.     int dirac_unit_size;
  53.     uint8_t *dirac_unit;
  54. } DiracParseContext;
  55.  
  56. static int find_frame_end(DiracParseContext *pc,
  57.                           const uint8_t *buf, int buf_size)
  58. {
  59.     uint32_t state = pc->state;
  60.     int i = 0;
  61.  
  62.     if (!pc->is_synced) {
  63.         for (i = 0; i < buf_size; i++) {
  64.             state = (state << 8) | buf[i];
  65.             if (state == DIRAC_PARSE_INFO_PREFIX) {
  66.                 state                   = -1;
  67.                 pc->is_synced           = 1;
  68.                 pc->header_bytes_needed = 9;
  69.                 pc->sync_offset         = i;
  70.                 break;
  71.             }
  72.         }
  73.     }
  74.  
  75.     if (pc->is_synced) {
  76.         pc->sync_offset = 0;
  77.         for (; i < buf_size; i++) {
  78.             if (state == DIRAC_PARSE_INFO_PREFIX) {
  79.                 if ((buf_size - i) >= pc->header_bytes_needed) {
  80.                     pc->state = -1;
  81.                     return i + pc->header_bytes_needed;
  82.                 } else {
  83.                     pc->header_bytes_needed = 9 - (buf_size - i);
  84.                     break;
  85.                 }
  86.             } else
  87.                 state = (state << 8) | buf[i];
  88.         }
  89.     }
  90.     pc->state = state;
  91.     return -1;
  92. }
  93.  
  94. typedef struct DiracParseUnit {
  95.     int next_pu_offset;
  96.     int prev_pu_offset;
  97.     uint8_t pu_type;
  98. } DiracParseUnit;
  99.  
  100. static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
  101.                              int offset)
  102. {
  103.     int8_t *start;
  104.  
  105.     if (offset < 0 || pc->index - 13 < offset)
  106.         return 0;
  107.  
  108.     start = pc->buffer + offset;
  109.     pu->pu_type = start[4];
  110.  
  111.     pu->next_pu_offset = AV_RB32(start + 5);
  112.     pu->prev_pu_offset = AV_RB32(start + 9);
  113.  
  114.     if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
  115.         pu->next_pu_offset = 13;
  116.  
  117.     if (pu->next_pu_offset && pu->next_pu_offset < 13) {
  118.         av_log(NULL, AV_LOG_ERROR, "next_pu_offset %d is invalid\n", pu->next_pu_offset);
  119.         return 0;
  120.     }
  121.     if (pu->prev_pu_offset && pu->prev_pu_offset < 13) {
  122.         av_log(NULL, AV_LOG_ERROR, "prev_pu_offset %d is invalid\n", pu->prev_pu_offset);
  123.         return 0;
  124.     }
  125.  
  126.     return 1;
  127. }
  128.  
  129. static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
  130.                                int next, const uint8_t **buf, int *buf_size)
  131. {
  132.     int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
  133.                              s->dts == AV_NOPTS_VALUE);
  134.     DiracParseContext *pc = s->priv_data;
  135.  
  136.     if (pc->overread_index) {
  137.         memmove(pc->buffer, pc->buffer + pc->overread_index,
  138.                pc->index - pc->overread_index);
  139.         pc->index         -= pc->overread_index;
  140.         pc->overread_index = 0;
  141.         if (*buf_size == 0 && pc->buffer[4] == 0x10) {
  142.             *buf      = pc->buffer;
  143.             *buf_size = pc->index;
  144.             return 0;
  145.         }
  146.     }
  147.  
  148.     if (next == -1) {
  149.         /* Found a possible frame start but not a frame end */
  150.         void *new_buffer =
  151.             av_fast_realloc(pc->buffer, &pc->buffer_size,
  152.                             pc->index + (*buf_size - pc->sync_offset));
  153.         if (!new_buffer)
  154.             return AVERROR(ENOMEM);
  155.         pc->buffer = new_buffer;
  156.         memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
  157.                *buf_size - pc->sync_offset);
  158.         pc->index += *buf_size - pc->sync_offset;
  159.         return -1;
  160.     } else {
  161.         /* Found a possible frame start and a  possible frame end */
  162.         DiracParseUnit pu1, pu;
  163.         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  164.                                            pc->index + next);
  165.         if (!new_buffer)
  166.             return AVERROR(ENOMEM);
  167.         pc->buffer = new_buffer;
  168.         memcpy(pc->buffer + pc->index, *buf, next);
  169.         pc->index += next;
  170.  
  171.         /* Need to check if we have a valid Parse Unit. We can't go by the
  172.          * sync pattern 'BBCD' alone because arithmetic coding of the residual
  173.          * and motion data can cause the pattern triggering a false start of
  174.          * frame. So check if the previous parse offset of the next parse unit
  175.          * is equal to the next parse offset of the current parse unit then
  176.          * we can be pretty sure that we have a valid parse unit */
  177.         if (!unpack_parse_unit(&pu1, pc, pc->index - 13)                     ||
  178.             !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
  179.             pu.next_pu_offset != pu1.prev_pu_offset                          ||
  180.             pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
  181.         ) {
  182.             pc->index              -= 9;
  183.             *buf_size               = next - 9;
  184.             pc->header_bytes_needed = 9;
  185.             return -1;
  186.         }
  187.  
  188.         /* All non-frame data must be accompanied by frame data. This is to
  189.          * ensure that pts is set correctly. So if the current parse unit is
  190.          * not frame data, wait for frame data to come along */
  191.  
  192.         pc->dirac_unit = pc->buffer + pc->index - 13 -
  193.                          pu1.prev_pu_offset - pc->dirac_unit_size;
  194.  
  195.         pc->dirac_unit_size += pu.next_pu_offset;
  196.  
  197.         if ((pu.pu_type & 0x08) != 0x08) {
  198.             pc->header_bytes_needed = 9;
  199.             *buf_size               = next;
  200.             return -1;
  201.         }
  202.  
  203.         /* Get the picture number to set the pts and dts*/
  204.         if (parse_timing_info && pu1.prev_pu_offset >= 13) {
  205.             uint8_t *cur_pu = pc->buffer +
  206.                               pc->index - 13 - pu1.prev_pu_offset;
  207.             int pts = AV_RB32(cur_pu + 13);
  208.             if (s->last_pts == 0 && s->last_dts == 0)
  209.                 s->dts = pts - 1;
  210.             else
  211.                 s->dts = s->last_dts + 1;
  212.             s->pts = pts;
  213.             if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
  214.                 avctx->has_b_frames = 1;
  215.         }
  216.         if (avctx->has_b_frames && s->pts == s->dts)
  217.             s->pict_type = AV_PICTURE_TYPE_B;
  218.  
  219.         /* Finally have a complete Dirac data unit */
  220.         *buf      = pc->dirac_unit;
  221.         *buf_size = pc->dirac_unit_size;
  222.  
  223.         pc->dirac_unit_size     = 0;
  224.         pc->overread_index      = pc->index - 13;
  225.         pc->header_bytes_needed = 9;
  226.     }
  227.     return next;
  228. }
  229.  
  230. static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  231.                        const uint8_t **poutbuf, int *poutbuf_size,
  232.                        const uint8_t *buf, int buf_size)
  233. {
  234.     DiracParseContext *pc = s->priv_data;
  235.     int next;
  236.  
  237.     *poutbuf      = NULL;
  238.     *poutbuf_size = 0;
  239.  
  240.     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  241.         next          = buf_size;
  242.         *poutbuf      = buf;
  243.         *poutbuf_size = buf_size;
  244.         /* Assume that data has been packetized into an encapsulation unit. */
  245.     } else {
  246.         next = find_frame_end(pc, buf, buf_size);
  247.         if (!pc->is_synced && next == -1)
  248.             /* No frame start found yet. So throw away the entire buffer. */
  249.             return buf_size;
  250.  
  251.         if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
  252.             return buf_size;
  253.     }
  254.  
  255.     *poutbuf      = buf;
  256.     *poutbuf_size = buf_size;
  257.     return next;
  258. }
  259.  
  260. static void dirac_parse_close(AVCodecParserContext *s)
  261. {
  262.     DiracParseContext *pc = s->priv_data;
  263.  
  264.     if (pc->buffer_size > 0)
  265.         av_freep(&pc->buffer);
  266. }
  267.  
  268. AVCodecParser ff_dirac_parser = {
  269.     .codec_ids      = { AV_CODEC_ID_DIRAC },
  270.     .priv_data_size = sizeof(DiracParseContext),
  271.     .parser_parse   = dirac_parse,
  272.     .parser_close   = dirac_parse_close,
  273. };
  274.