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.

311 lines
9.4KB

  1. /*
  2. * Audio and Video frame extraction
  3. * Copyright (c) 2003 Fabrice Bellard
  4. * Copyright (c) 2003 Michael Niedermayer
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "libavutil/mem.h"
  25. #include "internal.h"
  26. #include "parser.h"
  27. static AVCodecParser *av_first_parser = NULL;
  28. AVCodecParser *av_parser_next(const AVCodecParser *p)
  29. {
  30. if (p)
  31. return p->next;
  32. else
  33. return av_first_parser;
  34. }
  35. void av_register_codec_parser(AVCodecParser *parser)
  36. {
  37. parser->next = av_first_parser;
  38. av_first_parser = parser;
  39. }
  40. AVCodecParserContext *av_parser_init(int codec_id)
  41. {
  42. AVCodecParserContext *s;
  43. AVCodecParser *parser;
  44. int ret;
  45. if (codec_id == AV_CODEC_ID_NONE)
  46. return NULL;
  47. for (parser = av_first_parser; parser != NULL; parser = parser->next) {
  48. if (parser->codec_ids[0] == codec_id ||
  49. parser->codec_ids[1] == codec_id ||
  50. parser->codec_ids[2] == codec_id ||
  51. parser->codec_ids[3] == codec_id ||
  52. parser->codec_ids[4] == codec_id)
  53. goto found;
  54. }
  55. return NULL;
  56. found:
  57. s = av_mallocz(sizeof(AVCodecParserContext));
  58. if (!s)
  59. return NULL;
  60. s->parser = parser;
  61. if (parser->priv_data_size) {
  62. s->priv_data = av_mallocz(parser->priv_data_size);
  63. if (!s->priv_data) {
  64. av_free(s);
  65. return NULL;
  66. }
  67. }
  68. if (parser->parser_init) {
  69. ret = parser->parser_init(s);
  70. if (ret != 0) {
  71. av_free(s->priv_data);
  72. av_free(s);
  73. return NULL;
  74. }
  75. }
  76. s->fetch_timestamp = 1;
  77. s->pict_type = AV_PICTURE_TYPE_I;
  78. s->key_frame = -1;
  79. s->convergence_duration = 0;
  80. s->dts_sync_point = INT_MIN;
  81. s->dts_ref_dts_delta = INT_MIN;
  82. s->pts_dts_delta = INT_MIN;
  83. s->format = -1;
  84. return s;
  85. }
  86. void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove)
  87. {
  88. int i;
  89. s->dts =
  90. s->pts = AV_NOPTS_VALUE;
  91. s->pos = -1;
  92. s->offset = 0;
  93. for (i = 0; i < AV_PARSER_PTS_NB; i++) {
  94. if (s->cur_offset + off >= s->cur_frame_offset[i] &&
  95. (s->frame_offset < s->cur_frame_offset[i] ||
  96. (!s->frame_offset && !s->next_frame_offset)) &&
  97. s->cur_frame_end[i]) {
  98. s->dts = s->cur_frame_dts[i];
  99. s->pts = s->cur_frame_pts[i];
  100. s->pos = s->cur_frame_pos[i];
  101. s->offset = s->next_frame_offset - s->cur_frame_offset[i];
  102. if (remove)
  103. s->cur_frame_offset[i] = INT64_MAX;
  104. if (s->cur_offset + off < s->cur_frame_end[i])
  105. break;
  106. }
  107. }
  108. }
  109. int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
  110. uint8_t **poutbuf, int *poutbuf_size,
  111. const uint8_t *buf, int buf_size,
  112. int64_t pts, int64_t dts, int64_t pos)
  113. {
  114. int index, i;
  115. uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
  116. if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
  117. s->next_frame_offset =
  118. s->cur_offset = pos;
  119. s->flags |= PARSER_FLAG_FETCHED_OFFSET;
  120. }
  121. if (buf_size == 0) {
  122. /* padding is always necessary even if EOF, so we add it here */
  123. memset(dummy_buf, 0, sizeof(dummy_buf));
  124. buf = dummy_buf;
  125. } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
  126. /* add a new packet descriptor */
  127. i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  128. s->cur_frame_start_index = i;
  129. s->cur_frame_offset[i] = s->cur_offset;
  130. s->cur_frame_end[i] = s->cur_offset + buf_size;
  131. s->cur_frame_pts[i] = pts;
  132. s->cur_frame_dts[i] = dts;
  133. s->cur_frame_pos[i] = pos;
  134. }
  135. if (s->fetch_timestamp) {
  136. s->fetch_timestamp = 0;
  137. s->last_pts = s->pts;
  138. s->last_dts = s->dts;
  139. s->last_pos = s->pos;
  140. ff_fetch_timestamp(s, 0, 0);
  141. }
  142. /* WARNING: the returned index can be negative */
  143. index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
  144. poutbuf_size, buf, buf_size);
  145. /* update the file pointer */
  146. if (*poutbuf_size) {
  147. /* fill the data for the current frame */
  148. s->frame_offset = s->next_frame_offset;
  149. /* offset of the next frame */
  150. s->next_frame_offset = s->cur_offset + index;
  151. s->fetch_timestamp = 1;
  152. }
  153. if (index < 0)
  154. index = 0;
  155. s->cur_offset += index;
  156. return index;
  157. }
  158. int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
  159. uint8_t **poutbuf, int *poutbuf_size,
  160. const uint8_t *buf, int buf_size, int keyframe)
  161. {
  162. if (s && s->parser->split) {
  163. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
  164. avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
  165. int i = s->parser->split(avctx, buf, buf_size);
  166. buf += i;
  167. buf_size -= i;
  168. }
  169. }
  170. /* cast to avoid warning about discarding qualifiers */
  171. *poutbuf = (uint8_t *) buf;
  172. *poutbuf_size = buf_size;
  173. if (avctx->extradata) {
  174. if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
  175. int size = buf_size + avctx->extradata_size;
  176. *poutbuf_size = size;
  177. *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  178. if (!*poutbuf)
  179. return AVERROR(ENOMEM);
  180. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  181. memcpy(*poutbuf + avctx->extradata_size, buf,
  182. buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  183. return 1;
  184. }
  185. }
  186. return 0;
  187. }
  188. void av_parser_close(AVCodecParserContext *s)
  189. {
  190. if (s) {
  191. if (s->parser->parser_close)
  192. s->parser->parser_close(s);
  193. av_free(s->priv_data);
  194. av_free(s);
  195. }
  196. }
  197. int ff_combine_frame(ParseContext *pc, int next,
  198. const uint8_t **buf, int *buf_size)
  199. {
  200. if (pc->overread) {
  201. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  202. pc->overread, pc->state, next, pc->index, pc->overread_index);
  203. ff_dlog(NULL, "%X %X %X %X\n",
  204. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  205. }
  206. /* Copy overread bytes from last frame into buffer. */
  207. for (; pc->overread > 0; pc->overread--)
  208. pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
  209. /* flush remaining if EOF */
  210. if (!*buf_size && next == END_NOT_FOUND)
  211. next = 0;
  212. pc->last_index = pc->index;
  213. /* copy into buffer end return */
  214. if (next == END_NOT_FOUND) {
  215. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  216. *buf_size + pc->index +
  217. AV_INPUT_BUFFER_PADDING_SIZE);
  218. if (!new_buffer)
  219. return AVERROR(ENOMEM);
  220. pc->buffer = new_buffer;
  221. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  222. pc->index += *buf_size;
  223. return -1;
  224. }
  225. *buf_size =
  226. pc->overread_index = pc->index + next;
  227. /* append to buffer */
  228. if (pc->index) {
  229. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  230. next + pc->index +
  231. AV_INPUT_BUFFER_PADDING_SIZE);
  232. if (!new_buffer)
  233. return AVERROR(ENOMEM);
  234. pc->buffer = new_buffer;
  235. if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
  236. memcpy(&pc->buffer[pc->index], *buf,
  237. next + AV_INPUT_BUFFER_PADDING_SIZE);
  238. pc->index = 0;
  239. *buf = pc->buffer;
  240. }
  241. /* store overread bytes */
  242. for (; next < 0; next++) {
  243. pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
  244. pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
  245. pc->overread++;
  246. }
  247. if (pc->overread) {
  248. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  249. pc->overread, pc->state, next, pc->index, pc->overread_index);
  250. ff_dlog(NULL, "%X %X %X %X\n",
  251. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  252. }
  253. return 0;
  254. }
  255. void ff_parse_close(AVCodecParserContext *s)
  256. {
  257. ParseContext *pc = s->priv_data;
  258. av_freep(&pc->buffer);
  259. }
  260. int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  261. {
  262. int i;
  263. uint32_t state = -1;
  264. for (i = 0; i < buf_size; i++) {
  265. state = state << 8 | buf[i];
  266. if (state == 0x1B3 || state == 0x1B6)
  267. return i - 3;
  268. }
  269. return 0;
  270. }