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.

346 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. av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
  128. /* Parsers only work for the specified codec ids. */
  129. av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
  130. avctx->codec_id == s->parser->codec_ids[1] ||
  131. avctx->codec_id == s->parser->codec_ids[2] ||
  132. avctx->codec_id == s->parser->codec_ids[3] ||
  133. avctx->codec_id == s->parser->codec_ids[4]);
  134. if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
  135. s->next_frame_offset =
  136. s->cur_offset = pos;
  137. s->flags |= PARSER_FLAG_FETCHED_OFFSET;
  138. }
  139. if (buf_size == 0) {
  140. /* padding is always necessary even if EOF, so we add it here */
  141. memset(dummy_buf, 0, sizeof(dummy_buf));
  142. buf = dummy_buf;
  143. } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
  144. /* add a new packet descriptor */
  145. i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  146. s->cur_frame_start_index = i;
  147. s->cur_frame_offset[i] = s->cur_offset;
  148. s->cur_frame_end[i] = s->cur_offset + buf_size;
  149. s->cur_frame_pts[i] = pts;
  150. s->cur_frame_dts[i] = dts;
  151. s->cur_frame_pos[i] = pos;
  152. }
  153. if (s->fetch_timestamp) {
  154. s->fetch_timestamp = 0;
  155. s->last_pts = s->pts;
  156. s->last_dts = s->dts;
  157. s->last_pos = s->pos;
  158. ff_fetch_timestamp(s, 0, 0, 0);
  159. }
  160. /* WARNING: the returned index can be negative */
  161. index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
  162. poutbuf_size, buf, buf_size);
  163. av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
  164. #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
  165. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  166. FILL(field_order);
  167. }
  168. /* update the file pointer */
  169. if (*poutbuf_size) {
  170. /* fill the data for the current frame */
  171. s->frame_offset = s->next_frame_offset;
  172. /* offset of the next frame */
  173. s->next_frame_offset = s->cur_offset + index;
  174. s->fetch_timestamp = 1;
  175. }
  176. if (index < 0)
  177. index = 0;
  178. s->cur_offset += index;
  179. return index;
  180. }
  181. int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
  182. uint8_t **poutbuf, int *poutbuf_size,
  183. const uint8_t *buf, int buf_size, int keyframe)
  184. {
  185. if (s && s->parser->split) {
  186. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
  187. avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
  188. int i = s->parser->split(avctx, buf, buf_size);
  189. buf += i;
  190. buf_size -= i;
  191. }
  192. }
  193. /* cast to avoid warning about discarding qualifiers */
  194. *poutbuf = (uint8_t *) buf;
  195. *poutbuf_size = buf_size;
  196. if (avctx->extradata) {
  197. if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
  198. int size = buf_size + avctx->extradata_size;
  199. *poutbuf_size = size;
  200. *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  201. if (!*poutbuf)
  202. return AVERROR(ENOMEM);
  203. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  204. memcpy(*poutbuf + avctx->extradata_size, buf,
  205. buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  206. return 1;
  207. }
  208. }
  209. return 0;
  210. }
  211. void av_parser_close(AVCodecParserContext *s)
  212. {
  213. if (s) {
  214. if (s->parser->parser_close)
  215. s->parser->parser_close(s);
  216. av_freep(&s->priv_data);
  217. av_free(s);
  218. }
  219. }
  220. int ff_combine_frame(ParseContext *pc, int next,
  221. const uint8_t **buf, int *buf_size)
  222. {
  223. if (pc->overread) {
  224. ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
  225. pc->overread, pc->state, next, pc->index, pc->overread_index);
  226. ff_dlog(NULL, "%X %X %X %X\n",
  227. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  228. }
  229. /* Copy overread bytes from last frame into buffer. */
  230. for (; pc->overread > 0; pc->overread--)
  231. pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
  232. /* flush remaining if EOF */
  233. if (!*buf_size && next == END_NOT_FOUND)
  234. next = 0;
  235. pc->last_index = pc->index;
  236. /* copy into buffer end return */
  237. if (next == END_NOT_FOUND) {
  238. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  239. *buf_size + pc->index +
  240. AV_INPUT_BUFFER_PADDING_SIZE);
  241. if (!new_buffer) {
  242. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
  243. pc->index = 0;
  244. return AVERROR(ENOMEM);
  245. }
  246. pc->buffer = new_buffer;
  247. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  248. pc->index += *buf_size;
  249. return -1;
  250. }
  251. *buf_size =
  252. pc->overread_index = pc->index + next;
  253. /* append to buffer */
  254. if (pc->index) {
  255. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  256. next + pc->index +
  257. AV_INPUT_BUFFER_PADDING_SIZE);
  258. if (!new_buffer) {
  259. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
  260. pc->overread_index =
  261. pc->index = 0;
  262. return AVERROR(ENOMEM);
  263. }
  264. pc->buffer = new_buffer;
  265. if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
  266. memcpy(&pc->buffer[pc->index], *buf,
  267. next + AV_INPUT_BUFFER_PADDING_SIZE);
  268. pc->index = 0;
  269. *buf = pc->buffer;
  270. }
  271. /* store overread bytes */
  272. for (; next < 0; next++) {
  273. pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
  274. pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
  275. pc->overread++;
  276. }
  277. if (pc->overread) {
  278. ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
  279. pc->overread, pc->state, next, pc->index, pc->overread_index);
  280. ff_dlog(NULL, "%X %X %X %X\n",
  281. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  282. }
  283. return 0;
  284. }
  285. void ff_parse_close(AVCodecParserContext *s)
  286. {
  287. ParseContext *pc = s->priv_data;
  288. av_freep(&pc->buffer);
  289. }
  290. int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  291. {
  292. uint32_t state = -1;
  293. const uint8_t *ptr = buf, *end = buf + buf_size;
  294. while (ptr < end) {
  295. ptr = avpriv_find_start_code(ptr, end, &state);
  296. if (state == 0x1B3 || state == 0x1B6)
  297. return ptr - 4 - buf;
  298. }
  299. return 0;
  300. }