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.

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