dirac_parser.c 8.1 KB

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