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.

317 lines
9.7KB

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