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.

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