You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.0KB

  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. * @file dirac_parser.c
  25. * Dirac Parser
  26. * @author Marco Gerards <marco@gnu.org>
  27. */
  28. #include "parser.h"
  29. #define DIRAC_PARSE_INFO_PREFIX 0x42424344
  30. /**
  31. * Finds the end of the current frame in the bitstream.
  32. * @return the position of the first byte of the next frame or -1
  33. */
  34. typedef struct DiracParseContext {
  35. int state;
  36. int is_synced;
  37. int sync_offset;
  38. int header_bytes_needed;
  39. int overread_index;
  40. int buffer_size;
  41. int index;
  42. uint8_t *buffer;
  43. int dirac_unit_size;
  44. uint8_t *dirac_unit;
  45. } DiracParseContext;
  46. static int find_frame_end(DiracParseContext *pc,
  47. const uint8_t *buf, int buf_size)
  48. {
  49. uint32_t state = pc->state;
  50. int i = 0;
  51. if (!pc->is_synced) {
  52. for (i = 0; i < buf_size; i++) {
  53. state = (state << 8) | buf[i];
  54. if (state == DIRAC_PARSE_INFO_PREFIX) {
  55. state = -1;
  56. pc->is_synced = 1;
  57. pc->header_bytes_needed = 9;
  58. pc->sync_offset = i;
  59. break;
  60. }
  61. }
  62. }
  63. if (pc->is_synced) {
  64. pc->sync_offset = 0;
  65. for (; i < buf_size; i++) {
  66. if (state == DIRAC_PARSE_INFO_PREFIX) {
  67. if ((buf_size-i) >= pc->header_bytes_needed) {
  68. pc->state = -1;
  69. return i + pc->header_bytes_needed;
  70. } else {
  71. pc->header_bytes_needed = 9-(buf_size-i);
  72. break;
  73. }
  74. } else
  75. state = (state << 8) | buf[i];
  76. }
  77. }
  78. pc->state = state;
  79. return -1;
  80. }
  81. typedef struct DiracParseUnit
  82. {
  83. int next_pu_offset;
  84. int prev_pu_offset;
  85. uint8_t pu_type;
  86. } DiracParseUnit;
  87. static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
  88. int offset)
  89. {
  90. uint8_t *start = pc->buffer + offset;
  91. uint8_t *end = pc->buffer + pc->index;
  92. if (start < pc->buffer || (start+13 > end))
  93. return 0;
  94. pu->pu_type = start[4];
  95. pu->next_pu_offset = AV_RB32(start+5);
  96. pu->prev_pu_offset = AV_RB32(start+9);
  97. if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
  98. pu->next_pu_offset = 13;
  99. return 1;
  100. }
  101. static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
  102. int next, const uint8_t **buf, int *buf_size)
  103. {
  104. int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
  105. s->dts == AV_NOPTS_VALUE);
  106. DiracParseContext *pc = s->priv_data;
  107. if (pc->overread_index) {
  108. memcpy(pc->buffer, pc->buffer + pc->overread_index,
  109. pc->index - pc->overread_index);
  110. pc->index -= pc->overread_index;
  111. pc->overread_index = 0;
  112. if (*buf_size == 0 && pc->buffer[4] == 0x10) {
  113. *buf = pc->buffer;
  114. *buf_size = pc->index;
  115. return 0;
  116. }
  117. }
  118. if ( next == -1) {
  119. /* Found a possible frame start but not a frame end */
  120. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  121. pc->index + (*buf_size -
  122. pc->sync_offset));
  123. pc->buffer = new_buffer;
  124. memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
  125. *buf_size - pc->sync_offset);
  126. pc->index += *buf_size - pc->sync_offset;
  127. return -1;
  128. } else {
  129. /* Found a possible frame start and a possible frame end */
  130. DiracParseUnit pu1, pu;
  131. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  132. pc->index + next);
  133. pc->buffer = new_buffer;
  134. memcpy(pc->buffer + pc->index, *buf, next);
  135. pc->index += next;
  136. /* Need to check if we have a valid Parse Unit. We can't go by the
  137. * sync pattern 'BBCD' alone because arithmetic coding of the residual
  138. * and motion data can cause the pattern triggering a false start of
  139. * frame. So check if the previous parse offset of the next parse unit
  140. * is equal to the next parse offset of the current parse unit then
  141. * we can be pretty sure that we have a valid parse unit */
  142. if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
  143. !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
  144. pu.next_pu_offset != pu1.prev_pu_offset) {
  145. pc->index -= 9;
  146. *buf_size = next-9;
  147. pc->header_bytes_needed = 9;
  148. return -1;
  149. }
  150. /* All non-frame data must be accompanied by frame data. This is to
  151. * ensure that pts is set correctly. So if the current parse unit is
  152. * not frame data, wait for frame data to come along */
  153. pc->dirac_unit = pc->buffer + pc->index - 13 -
  154. pu1.prev_pu_offset - pc->dirac_unit_size;
  155. pc->dirac_unit_size += pu.next_pu_offset;
  156. if ((pu.pu_type&0x08) != 0x08) {
  157. pc->header_bytes_needed = 9;
  158. *buf_size = next;
  159. return -1;
  160. }
  161. /* Get the picture number to set the pts and dts*/
  162. if (parse_timing_info) {
  163. uint8_t *cur_pu = pc->buffer +
  164. pc->index - 13 - pu1.prev_pu_offset;
  165. int pts = AV_RB32(cur_pu + 13);
  166. if (s->last_pts == 0 && s->last_dts == 0)
  167. s->dts = pts - 1;
  168. else
  169. s->dts = s->last_dts+1;
  170. s->pts = pts;
  171. if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
  172. avctx->has_b_frames = 1;
  173. }
  174. if (avctx->has_b_frames && s->pts == s->dts)
  175. s->pict_type = FF_B_TYPE;
  176. /* Finally have a complete Dirac data unit */
  177. *buf = pc->dirac_unit;
  178. *buf_size = pc->dirac_unit_size;
  179. pc->dirac_unit_size = 0;
  180. pc->overread_index = pc->index-13;
  181. pc->header_bytes_needed = 9;
  182. }
  183. return next;
  184. }
  185. static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  186. const uint8_t **poutbuf, int *poutbuf_size,
  187. const uint8_t *buf, int buf_size)
  188. {
  189. DiracParseContext *pc = s->priv_data;
  190. int next;
  191. *poutbuf = NULL;
  192. *poutbuf_size = 0;
  193. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  194. next = buf_size;
  195. *poutbuf = buf;
  196. *poutbuf_size = buf_size;
  197. /* Assume that data has been packetized into an encapsulation unit. */
  198. } else {
  199. next = find_frame_end(pc, buf, buf_size);
  200. if (!pc->is_synced && next == -1) {
  201. /* No frame start found yet. So throw away the entire buffer. */
  202. return buf_size;
  203. }
  204. if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
  205. return buf_size;
  206. }
  207. }
  208. *poutbuf = buf;
  209. *poutbuf_size = buf_size;
  210. return next;
  211. }
  212. static void dirac_parse_close(AVCodecParserContext *s)
  213. {
  214. DiracParseContext *pc = s->priv_data;
  215. if (pc->buffer_size > 0)
  216. av_free(pc->buffer);
  217. }
  218. AVCodecParser dirac_parser = {
  219. { CODEC_ID_DIRAC },
  220. sizeof(DiracParseContext),
  221. NULL,
  222. dirac_parse,
  223. dirac_parse_close,
  224. };