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.

327 lines
10KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avassert.h"
  25. #include "libavutil/atomic.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. do {
  40. parser->next = av_first_parser;
  41. } while (parser->next != avpriv_atomic_ptr_cas((void * volatile *)&av_first_parser, parser->next, parser));
  42. }
  43. AVCodecParserContext *av_parser_init(int codec_id)
  44. {
  45. AVCodecParserContext *s = NULL;
  46. AVCodecParser *parser;
  47. int ret;
  48. if (codec_id == AV_CODEC_ID_NONE)
  49. return NULL;
  50. for (parser = av_first_parser; parser; parser = parser->next) {
  51. if (parser->codec_ids[0] == codec_id ||
  52. parser->codec_ids[1] == codec_id ||
  53. parser->codec_ids[2] == codec_id ||
  54. parser->codec_ids[3] == codec_id ||
  55. parser->codec_ids[4] == codec_id)
  56. goto found;
  57. }
  58. return NULL;
  59. found:
  60. s = av_mallocz(sizeof(AVCodecParserContext));
  61. if (!s)
  62. goto err_out;
  63. s->parser = parser;
  64. s->priv_data = av_mallocz(parser->priv_data_size);
  65. if (!s->priv_data)
  66. goto err_out;
  67. s->fetch_timestamp=1;
  68. s->pict_type = AV_PICTURE_TYPE_I;
  69. if (parser->parser_init) {
  70. ret = parser->parser_init(s);
  71. if (ret != 0)
  72. goto err_out;
  73. }
  74. s->key_frame = -1;
  75. s->convergence_duration = 0;
  76. s->dts_sync_point = INT_MIN;
  77. s->dts_ref_dts_delta = INT_MIN;
  78. s->pts_dts_delta = INT_MIN;
  79. s->format = -1;
  80. return s;
  81. err_out:
  82. if (s)
  83. av_freep(&s->priv_data);
  84. av_free(s);
  85. return NULL;
  86. }
  87. void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
  88. {
  89. int i;
  90. if (!fuzzy) {
  91. s->dts =
  92. s->pts = AV_NOPTS_VALUE;
  93. s->pos = -1;
  94. s->offset = 0;
  95. }
  96. for (i = 0; i < AV_PARSER_PTS_NB; i++) {
  97. if (s->cur_offset + off >= s->cur_frame_offset[i] &&
  98. (s->frame_offset < s->cur_frame_offset[i] ||
  99. (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
  100. // check disabled since MPEG-TS does not send complete PES packets
  101. /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
  102. if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
  103. s->dts = s->cur_frame_dts[i];
  104. s->pts = s->cur_frame_pts[i];
  105. s->pos = s->cur_frame_pos[i];
  106. s->offset = s->next_frame_offset - s->cur_frame_offset[i];
  107. }
  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[FF_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, 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. av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
  152. /* update the file pointer */
  153. if (*poutbuf_size) {
  154. /* fill the data for the current frame */
  155. s->frame_offset = s->next_frame_offset;
  156. /* offset of the next frame */
  157. s->next_frame_offset = s->cur_offset + index;
  158. s->fetch_timestamp = 1;
  159. }
  160. if (index < 0)
  161. index = 0;
  162. s->cur_offset += index;
  163. return index;
  164. }
  165. int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
  166. uint8_t **poutbuf, int *poutbuf_size,
  167. const uint8_t *buf, int buf_size, int keyframe)
  168. {
  169. if (s && s->parser->split) {
  170. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
  171. avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
  172. int i = s->parser->split(avctx, buf, buf_size);
  173. buf += i;
  174. buf_size -= i;
  175. }
  176. }
  177. /* cast to avoid warning about discarding qualifiers */
  178. *poutbuf = (uint8_t *) buf;
  179. *poutbuf_size = buf_size;
  180. if (avctx->extradata) {
  181. if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
  182. int size = buf_size + avctx->extradata_size;
  183. *poutbuf_size = size;
  184. *poutbuf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  185. if (!*poutbuf)
  186. return AVERROR(ENOMEM);
  187. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  188. memcpy(*poutbuf + avctx->extradata_size, buf,
  189. buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  190. return 1;
  191. }
  192. }
  193. return 0;
  194. }
  195. void av_parser_close(AVCodecParserContext *s)
  196. {
  197. if (s) {
  198. if (s->parser->parser_close)
  199. s->parser->parser_close(s);
  200. av_freep(&s->priv_data);
  201. av_free(s);
  202. }
  203. }
  204. int ff_combine_frame(ParseContext *pc, int next,
  205. const uint8_t **buf, int *buf_size)
  206. {
  207. if (pc->overread) {
  208. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  209. pc->overread, pc->state, next, pc->index, pc->overread_index);
  210. ff_dlog(NULL, "%X %X %X %X\n",
  211. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  212. }
  213. /* Copy overread bytes from last frame into buffer. */
  214. for (; pc->overread > 0; pc->overread--)
  215. pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
  216. /* flush remaining if EOF */
  217. if (!*buf_size && next == END_NOT_FOUND)
  218. next = 0;
  219. pc->last_index = pc->index;
  220. /* copy into buffer end return */
  221. if (next == END_NOT_FOUND) {
  222. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  223. *buf_size + pc->index +
  224. FF_INPUT_BUFFER_PADDING_SIZE);
  225. if (!new_buffer) {
  226. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  227. pc->index = 0;
  228. return AVERROR(ENOMEM);
  229. }
  230. pc->buffer = new_buffer;
  231. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  232. pc->index += *buf_size;
  233. return -1;
  234. }
  235. *buf_size =
  236. pc->overread_index = pc->index + next;
  237. /* append to buffer */
  238. if (pc->index) {
  239. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  240. next + pc->index +
  241. FF_INPUT_BUFFER_PADDING_SIZE);
  242. if (!new_buffer) {
  243. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  244. pc->overread_index =
  245. pc->index = 0;
  246. return AVERROR(ENOMEM);
  247. }
  248. pc->buffer = new_buffer;
  249. if (next > -FF_INPUT_BUFFER_PADDING_SIZE)
  250. memcpy(&pc->buffer[pc->index], *buf,
  251. next + FF_INPUT_BUFFER_PADDING_SIZE);
  252. pc->index = 0;
  253. *buf = pc->buffer;
  254. }
  255. /* store overread bytes */
  256. for (; next < 0; next++) {
  257. pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
  258. pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
  259. pc->overread++;
  260. }
  261. if (pc->overread) {
  262. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  263. pc->overread, pc->state, next, pc->index, pc->overread_index);
  264. ff_dlog(NULL, "%X %X %X %X\n",
  265. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  266. }
  267. return 0;
  268. }
  269. void ff_parse_close(AVCodecParserContext *s)
  270. {
  271. ParseContext *pc = s->priv_data;
  272. av_freep(&pc->buffer);
  273. }
  274. int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  275. {
  276. uint32_t state = -1;
  277. const uint8_t *ptr = buf, *end = buf + buf_size;
  278. while (ptr < end) {
  279. ptr = avpriv_find_start_code(ptr, end, &state);
  280. if (state == 0x1B3 || state == 0x1B6)
  281. return ptr - 4 - buf;
  282. }
  283. return 0;
  284. }