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.

317 lines
9.5KB

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