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.

316 lines
9.5KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/internal.h"
  25. #include "libavutil/mem.h"
  26. #include "internal.h"
  27. #include "parser.h"
  28. static AVCodecParser *av_first_parser = NULL;
  29. AVCodecParser *av_parser_next(const AVCodecParser *p)
  30. {
  31. if (p)
  32. return p->next;
  33. else
  34. return av_first_parser;
  35. }
  36. void av_register_codec_parser(AVCodecParser *parser)
  37. {
  38. parser->next = av_first_parser;
  39. av_first_parser = parser;
  40. }
  41. AVCodecParserContext *av_parser_init(int codec_id)
  42. {
  43. AVCodecParserContext *s;
  44. AVCodecParser *parser;
  45. int ret;
  46. if (codec_id == AV_CODEC_ID_NONE)
  47. return NULL;
  48. for (parser = av_first_parser; parser != NULL; parser = parser->next) {
  49. if (parser->codec_ids[0] == codec_id ||
  50. parser->codec_ids[1] == codec_id ||
  51. parser->codec_ids[2] == codec_id ||
  52. parser->codec_ids[3] == codec_id ||
  53. parser->codec_ids[4] == codec_id)
  54. goto found;
  55. }
  56. return NULL;
  57. found:
  58. s = av_mallocz(sizeof(AVCodecParserContext));
  59. if (!s)
  60. return NULL;
  61. s->parser = parser;
  62. if (parser->priv_data_size) {
  63. s->priv_data = av_mallocz(parser->priv_data_size);
  64. if (!s->priv_data) {
  65. av_free(s);
  66. return NULL;
  67. }
  68. }
  69. if (parser->parser_init) {
  70. ret = parser->parser_init(s);
  71. if (ret != 0) {
  72. av_free(s->priv_data);
  73. av_free(s);
  74. return NULL;
  75. }
  76. }
  77. s->fetch_timestamp = 1;
  78. s->pict_type = AV_PICTURE_TYPE_I;
  79. s->key_frame = -1;
  80. #if FF_API_CONVERGENCE_DURATION
  81. FF_DISABLE_DEPRECATION_WARNINGS
  82. s->convergence_duration = 0;
  83. FF_ENABLE_DEPRECATION_WARNINGS
  84. #endif
  85. s->dts_sync_point = INT_MIN;
  86. s->dts_ref_dts_delta = INT_MIN;
  87. s->pts_dts_delta = INT_MIN;
  88. s->format = -1;
  89. return s;
  90. }
  91. void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove)
  92. {
  93. int i;
  94. s->dts =
  95. s->pts = AV_NOPTS_VALUE;
  96. s->pos = -1;
  97. s->offset = 0;
  98. for (i = 0; i < AV_PARSER_PTS_NB; i++) {
  99. if (s->cur_offset + off >= s->cur_frame_offset[i] &&
  100. (s->frame_offset < s->cur_frame_offset[i] ||
  101. (!s->frame_offset && !s->next_frame_offset)) &&
  102. s->cur_frame_end[i]) {
  103. s->dts = s->cur_frame_dts[i];
  104. s->pts = s->cur_frame_pts[i];
  105. s->pos = s->cur_frame_pos[i];
  106. s->offset = s->next_frame_offset - s->cur_frame_offset[i];
  107. if (remove)
  108. s->cur_frame_offset[i] = INT64_MAX;
  109. if (s->cur_offset + off < s->cur_frame_end[i])
  110. break;
  111. }
  112. }
  113. }
  114. int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
  115. uint8_t **poutbuf, int *poutbuf_size,
  116. const uint8_t *buf, int buf_size,
  117. int64_t pts, int64_t dts, int64_t pos)
  118. {
  119. int index, i;
  120. uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
  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);
  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. /* update the file pointer */
  151. if (*poutbuf_size) {
  152. /* fill the data for the current frame */
  153. s->frame_offset = s->next_frame_offset;
  154. /* offset of the next frame */
  155. s->next_frame_offset = s->cur_offset + index;
  156. s->fetch_timestamp = 1;
  157. }
  158. if (index < 0)
  159. index = 0;
  160. s->cur_offset += index;
  161. return index;
  162. }
  163. int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
  164. uint8_t **poutbuf, int *poutbuf_size,
  165. const uint8_t *buf, int buf_size, int keyframe)
  166. {
  167. if (s && s->parser->split) {
  168. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
  169. avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
  170. int i = s->parser->split(avctx, buf, buf_size);
  171. buf += i;
  172. buf_size -= i;
  173. }
  174. }
  175. /* cast to avoid warning about discarding qualifiers */
  176. *poutbuf = (uint8_t *) buf;
  177. *poutbuf_size = buf_size;
  178. if (avctx->extradata) {
  179. if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
  180. int size = buf_size + avctx->extradata_size;
  181. *poutbuf_size = size;
  182. *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  183. if (!*poutbuf)
  184. return AVERROR(ENOMEM);
  185. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  186. memcpy(*poutbuf + avctx->extradata_size, buf,
  187. buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  188. return 1;
  189. }
  190. }
  191. return 0;
  192. }
  193. void av_parser_close(AVCodecParserContext *s)
  194. {
  195. if (s) {
  196. if (s->parser->parser_close)
  197. s->parser->parser_close(s);
  198. av_free(s->priv_data);
  199. av_free(s);
  200. }
  201. }
  202. int ff_combine_frame(ParseContext *pc, int next,
  203. const uint8_t **buf, int *buf_size)
  204. {
  205. if (pc->overread) {
  206. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  207. pc->overread, pc->state, next, pc->index, pc->overread_index);
  208. ff_dlog(NULL, "%X %X %X %X\n",
  209. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  210. }
  211. /* Copy overread bytes from last frame into buffer. */
  212. for (; pc->overread > 0; pc->overread--)
  213. pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
  214. /* flush remaining if EOF */
  215. if (!*buf_size && next == END_NOT_FOUND)
  216. next = 0;
  217. pc->last_index = pc->index;
  218. /* copy into buffer end return */
  219. if (next == END_NOT_FOUND) {
  220. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  221. *buf_size + pc->index +
  222. AV_INPUT_BUFFER_PADDING_SIZE);
  223. if (!new_buffer)
  224. return AVERROR(ENOMEM);
  225. pc->buffer = new_buffer;
  226. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  227. pc->index += *buf_size;
  228. return -1;
  229. }
  230. *buf_size =
  231. pc->overread_index = pc->index + next;
  232. /* append to buffer */
  233. if (pc->index) {
  234. void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
  235. next + pc->index +
  236. AV_INPUT_BUFFER_PADDING_SIZE);
  237. if (!new_buffer)
  238. return AVERROR(ENOMEM);
  239. pc->buffer = new_buffer;
  240. if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
  241. memcpy(&pc->buffer[pc->index], *buf,
  242. next + AV_INPUT_BUFFER_PADDING_SIZE);
  243. pc->index = 0;
  244. *buf = pc->buffer;
  245. }
  246. /* store overread bytes */
  247. for (; next < 0; next++) {
  248. pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
  249. pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
  250. pc->overread++;
  251. }
  252. if (pc->overread) {
  253. ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
  254. pc->overread, pc->state, next, pc->index, pc->overread_index);
  255. ff_dlog(NULL, "%X %X %X %X\n",
  256. (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
  257. }
  258. return 0;
  259. }
  260. void ff_parse_close(AVCodecParserContext *s)
  261. {
  262. ParseContext *pc = s->priv_data;
  263. av_freep(&pc->buffer);
  264. }
  265. int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  266. {
  267. int i;
  268. uint32_t state = -1;
  269. for (i = 0; i < buf_size; i++) {
  270. state = state << 8 | buf[i];
  271. if (state == 0x1B3 || state == 0x1B6)
  272. return i - 3;
  273. }
  274. return 0;
  275. }