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.

274 lines
8.7KB

  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. int8_t *start;
  93. if (offset < 0 || pc->index - 13 < offset)
  94. return 0;
  95. start = pc->buffer + offset;
  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. if (pu->next_pu_offset && pu->next_pu_offset < 13) {
  102. av_log(NULL, AV_LOG_ERROR, "next_pu_offset %d is invalid\n", pu->next_pu_offset);
  103. return 0;
  104. }
  105. if (pu->prev_pu_offset && pu->prev_pu_offset < 13) {
  106. av_log(NULL, AV_LOG_ERROR, "prev_pu_offset %d is invalid\n", pu->prev_pu_offset);
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
  112. int next, const uint8_t **buf, int *buf_size)
  113. {
  114. int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
  115. s->dts == AV_NOPTS_VALUE);
  116. DiracParseContext *pc = s->priv_data;
  117. if (pc->overread_index) {
  118. memmove(pc->buffer, pc->buffer + pc->overread_index,
  119. pc->index - pc->overread_index);
  120. pc->index -= pc->overread_index;
  121. pc->overread_index = 0;
  122. if (*buf_size == 0 && pc->buffer[4] == 0x10) {
  123. *buf = pc->buffer;
  124. *buf_size = pc->index;
  125. return 0;
  126. }
  127. }
  128. if (next == -1) {
  129. /* Found a possible frame start but not a frame end */
  130. void *new_buffer =
  131. av_fast_realloc(pc->buffer, &pc->buffer_size,
  132. pc->index + (*buf_size - pc->sync_offset));
  133. if (!new_buffer)
  134. return AVERROR(ENOMEM);
  135. pc->buffer = new_buffer;
  136. memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
  137. *buf_size - pc->sync_offset);
  138. pc->index += *buf_size - pc->sync_offset;
  139. return -1;
  140. } else {
  141. /* Found a possible frame start and a possible frame end */
  142. DiracParseUnit pu1, pu;
  143. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  144. pc->index + next);
  145. if (!new_buffer)
  146. return AVERROR(ENOMEM);
  147. pc->buffer = new_buffer;
  148. memcpy(pc->buffer + pc->index, *buf, next);
  149. pc->index += next;
  150. /* Need to check if we have a valid Parse Unit. We can't go by the
  151. * sync pattern 'BBCD' alone because arithmetic coding of the residual
  152. * and motion data can cause the pattern triggering a false start of
  153. * frame. So check if the previous parse offset of the next parse unit
  154. * is equal to the next parse offset of the current parse unit then
  155. * we can be pretty sure that we have a valid parse unit */
  156. if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
  157. !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
  158. pu.next_pu_offset != pu1.prev_pu_offset ||
  159. pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
  160. ) {
  161. pc->index -= 9;
  162. *buf_size = next - 9;
  163. pc->header_bytes_needed = 9;
  164. return -1;
  165. }
  166. /* All non-frame data must be accompanied by frame data. This is to
  167. * ensure that pts is set correctly. So if the current parse unit is
  168. * not frame data, wait for frame data to come along */
  169. pc->dirac_unit = pc->buffer + pc->index - 13 -
  170. pu1.prev_pu_offset - pc->dirac_unit_size;
  171. pc->dirac_unit_size += pu.next_pu_offset;
  172. if ((pu.pu_type & 0x08) != 0x08) {
  173. pc->header_bytes_needed = 9;
  174. *buf_size = next;
  175. return -1;
  176. }
  177. /* Get the picture number to set the pts and dts*/
  178. if (parse_timing_info && pu1.prev_pu_offset >= 13) {
  179. uint8_t *cur_pu = pc->buffer +
  180. pc->index - 13 - pu1.prev_pu_offset;
  181. int pts = AV_RB32(cur_pu + 13);
  182. if (s->last_pts == 0 && s->last_dts == 0)
  183. s->dts = pts - 1;
  184. else
  185. s->dts = s->last_dts + 1;
  186. s->pts = pts;
  187. if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
  188. avctx->has_b_frames = 1;
  189. }
  190. if (avctx->has_b_frames && s->pts == s->dts)
  191. s->pict_type = AV_PICTURE_TYPE_B;
  192. /* Finally have a complete Dirac data unit */
  193. *buf = pc->dirac_unit;
  194. *buf_size = pc->dirac_unit_size;
  195. pc->dirac_unit_size = 0;
  196. pc->overread_index = pc->index - 13;
  197. pc->header_bytes_needed = 9;
  198. }
  199. return next;
  200. }
  201. static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  202. const uint8_t **poutbuf, int *poutbuf_size,
  203. const uint8_t *buf, int buf_size)
  204. {
  205. DiracParseContext *pc = s->priv_data;
  206. int next;
  207. *poutbuf = NULL;
  208. *poutbuf_size = 0;
  209. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  210. next = buf_size;
  211. *poutbuf = buf;
  212. *poutbuf_size = buf_size;
  213. /* Assume that data has been packetized into an encapsulation unit. */
  214. } else {
  215. next = find_frame_end(pc, buf, buf_size);
  216. if (!pc->is_synced && next == -1)
  217. /* No frame start found yet. So throw away the entire buffer. */
  218. return buf_size;
  219. if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
  220. return buf_size;
  221. }
  222. *poutbuf = buf;
  223. *poutbuf_size = buf_size;
  224. return next;
  225. }
  226. static void dirac_parse_close(AVCodecParserContext *s)
  227. {
  228. DiracParseContext *pc = s->priv_data;
  229. if (pc->buffer_size > 0)
  230. av_freep(&pc->buffer);
  231. }
  232. AVCodecParser ff_dirac_parser = {
  233. .codec_ids = { AV_CODEC_ID_DIRAC },
  234. .priv_data_size = sizeof(DiracParseContext),
  235. .parser_parse = dirac_parse,
  236. .parser_close = dirac_parse_close,
  237. };