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