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.

263 lines
8.4KB

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