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.

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