parser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. }
  163. if (index < 0)
  164. index = 0;
  165. s->cur_offset += index;
  166. return index;
  167. }
  168. int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
  169. uint8_t **poutbuf, int *poutbuf_size,
  170. const uint8_t *buf, int buf_size, int keyframe)
  171. {
  172. if (s && s->parser->split) {
  173. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
  174. avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
  175. int i = s->parser->split(avctx, buf, buf_size);
  176. buf += i;
  177. buf_size -= i;
  178. }
  179. }
  180. /* cast to avoid warning about discarding qualifiers */
  181. *poutbuf = (uint8_t *) buf;
  182. *poutbuf_size = buf_size;
  183. if (avctx->extradata) {
  184. if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
  185. int size = buf_size + avctx->extradata_size;
  186. *poutbuf_size = size;
  187. *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  188. if (!*poutbuf)
  189. return AVERROR(ENOMEM);
  190. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  191. memcpy(*poutbuf + avctx->extradata_size, buf,
  192. buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  193. return 1;
  194. }
  195. }
  196. return 0;
  197. }
  198. void av_parser_close(AVCodecParserContext *s)
  199. {
  200. if (s) {
  201. if (s->parser->parser_close)
  202. s->parser->parser_close(s);
  203. av_freep(&s->priv_data);
  204. av_free(s);
  205. }
  206. }
  207. int ff_combine_frame(ParseContext *pc, int next,
  208. const uint8_t **buf, int *buf_size)
  209. {
  210. if (pc->overread) {
  211. ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
  212. pc->overread, pc->state, next, pc->index, pc->overread_index);
  213. ff_dlog(NULL, "%X %X %X %X\n",
  214. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  215. }
  216. /* Copy overread bytes from last frame into buffer. */
  217. for (; pc->overread > 0; pc->overread--)
  218. pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
  219. if (next > *buf_size)
  220. return AVERROR(EINVAL);
  221. /* flush remaining if EOF */
  222. if (!*buf_size && next == END_NOT_FOUND)
  223. next = 0;
  224. pc->last_index = pc->index;
  225. /* copy into buffer end return */
  226. if (next == END_NOT_FOUND) {
  227. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  228. *buf_size + pc->index +
  229. AV_INPUT_BUFFER_PADDING_SIZE);
  230. if (!new_buffer) {
  231. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
  232. pc->index = 0;
  233. return AVERROR(ENOMEM);
  234. }
  235. pc->buffer = new_buffer;
  236. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  237. pc->index += *buf_size;
  238. return -1;
  239. }
  240. av_assert0(next >= 0 || pc->buffer);
  241. *buf_size =
  242. pc->overread_index = pc->index + next;
  243. /* append to buffer */
  244. if (pc->index) {
  245. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  246. next + pc->index +
  247. AV_INPUT_BUFFER_PADDING_SIZE);
  248. if (!new_buffer) {
  249. av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
  250. pc->overread_index =
  251. pc->index = 0;
  252. return AVERROR(ENOMEM);
  253. }
  254. pc->buffer = new_buffer;
  255. if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
  256. memcpy(&pc->buffer[pc->index], *buf,
  257. next + AV_INPUT_BUFFER_PADDING_SIZE);
  258. pc->index = 0;
  259. *buf = pc->buffer;
  260. }
  261. /* store overread bytes */
  262. for (; next < 0; next++) {
  263. pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
  264. pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
  265. pc->overread++;
  266. }
  267. if (pc->overread) {
  268. ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
  269. pc->overread, pc->state, next, pc->index, pc->overread_index);
  270. ff_dlog(NULL, "%X %X %X %X\n",
  271. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  272. }
  273. return 0;
  274. }
  275. void ff_parse_close(AVCodecParserContext *s)
  276. {
  277. ParseContext *pc = s->priv_data;
  278. av_freep(&pc->buffer);
  279. }
  280. int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  281. {
  282. uint32_t state = -1;
  283. const uint8_t *ptr = buf, *end = buf + buf_size;
  284. while (ptr < end) {
  285. ptr = avpriv_find_start_code(ptr, end, &state);
  286. if (state == 0x1B3 || state == 0x1B6)
  287. return ptr - 4 - buf;
  288. }
  289. return 0;
  290. }