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.

339 lines
11KB

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