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.

257 lines
8.2KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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. pc->buffer = new_buffer;
  126. memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
  127. *buf_size - pc->sync_offset);
  128. pc->index += *buf_size - pc->sync_offset;
  129. return -1;
  130. } else {
  131. /* Found a possible frame start and a possible frame end */
  132. DiracParseUnit pu1, pu;
  133. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  134. pc->index + next);
  135. pc->buffer = new_buffer;
  136. memcpy(pc->buffer + pc->index, *buf, next);
  137. pc->index += next;
  138. /* Need to check if we have a valid Parse Unit. We can't go by the
  139. * sync pattern 'BBCD' alone because arithmetic coding of the residual
  140. * and motion data can cause the pattern triggering a false start of
  141. * frame. So check if the previous parse offset of the next parse unit
  142. * is equal to the next parse offset of the current parse unit then
  143. * we can be pretty sure that we have a valid parse unit */
  144. if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
  145. !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
  146. pu.next_pu_offset != pu1.prev_pu_offset) {
  147. pc->index -= 9;
  148. *buf_size = next - 9;
  149. pc->header_bytes_needed = 9;
  150. return -1;
  151. }
  152. /* All non-frame data must be accompanied by frame data. This is to
  153. * ensure that pts is set correctly. So if the current parse unit is
  154. * not frame data, wait for frame data to come along */
  155. pc->dirac_unit = pc->buffer + pc->index - 13 -
  156. pu1.prev_pu_offset - pc->dirac_unit_size;
  157. pc->dirac_unit_size += pu.next_pu_offset;
  158. if ((pu.pu_type & 0x08) != 0x08) {
  159. pc->header_bytes_needed = 9;
  160. *buf_size = next;
  161. return -1;
  162. }
  163. /* Get the picture number to set the pts and dts*/
  164. if (parse_timing_info) {
  165. uint8_t *cur_pu = pc->buffer +
  166. pc->index - 13 - pu1.prev_pu_offset;
  167. int pts = AV_RB32(cur_pu + 13);
  168. if (s->last_pts == 0 && s->last_dts == 0)
  169. s->dts = pts - 1;
  170. else
  171. s->dts = s->last_dts + 1;
  172. s->pts = pts;
  173. if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
  174. avctx->has_b_frames = 1;
  175. }
  176. if (avctx->has_b_frames && s->pts == s->dts)
  177. s->pict_type = AV_PICTURE_TYPE_B;
  178. /* Finally have a complete Dirac data unit */
  179. *buf = pc->dirac_unit;
  180. *buf_size = pc->dirac_unit_size;
  181. pc->dirac_unit_size = 0;
  182. pc->overread_index = pc->index - 13;
  183. pc->header_bytes_needed = 9;
  184. }
  185. return next;
  186. }
  187. static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  188. const uint8_t **poutbuf, int *poutbuf_size,
  189. const uint8_t *buf, int buf_size)
  190. {
  191. DiracParseContext *pc = s->priv_data;
  192. int next;
  193. *poutbuf = NULL;
  194. *poutbuf_size = 0;
  195. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  196. next = buf_size;
  197. *poutbuf = buf;
  198. *poutbuf_size = buf_size;
  199. /* Assume that data has been packetized into an encapsulation unit. */
  200. } else {
  201. next = find_frame_end(pc, buf, buf_size);
  202. if (!pc->is_synced && next == -1)
  203. /* No frame start found yet. So throw away the entire buffer. */
  204. return buf_size;
  205. if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
  206. return buf_size;
  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 ff_dirac_parser = {
  219. .codec_ids = { AV_CODEC_ID_DIRAC },
  220. .priv_data_size = sizeof(DiracParseContext),
  221. .parser_parse = dirac_parse,
  222. .parser_close = dirac_parse_close,
  223. };