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.

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