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.

312 lines
9.6KB

  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 "parser.h"
  23. AVCodecParser *av_first_parser = NULL;
  24. void av_register_codec_parser(AVCodecParser *parser)
  25. {
  26. parser->next = av_first_parser;
  27. av_first_parser = parser;
  28. }
  29. AVCodecParserContext *av_parser_init(int codec_id)
  30. {
  31. AVCodecParserContext *s;
  32. AVCodecParser *parser;
  33. int ret;
  34. if(codec_id == CODEC_ID_NONE)
  35. return NULL;
  36. for(parser = av_first_parser; parser != NULL; parser = parser->next) {
  37. if (parser->codec_ids[0] == codec_id ||
  38. parser->codec_ids[1] == codec_id ||
  39. parser->codec_ids[2] == codec_id ||
  40. parser->codec_ids[3] == codec_id ||
  41. parser->codec_ids[4] == codec_id)
  42. goto found;
  43. }
  44. return NULL;
  45. found:
  46. s = av_mallocz(sizeof(AVCodecParserContext));
  47. if (!s)
  48. return NULL;
  49. s->parser = parser;
  50. s->priv_data = av_mallocz(parser->priv_data_size);
  51. if (!s->priv_data) {
  52. av_free(s);
  53. return NULL;
  54. }
  55. if (parser->parser_init) {
  56. ret = parser->parser_init(s);
  57. if (ret != 0) {
  58. av_free(s->priv_data);
  59. av_free(s);
  60. return NULL;
  61. }
  62. }
  63. s->fetch_timestamp=1;
  64. s->pict_type = FF_I_TYPE;
  65. return s;
  66. }
  67. /**
  68. *
  69. * @param buf input
  70. * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output)
  71. * @param pts input presentation timestamp
  72. * @param dts input decoding timestamp
  73. * @param poutbuf will contain a pointer to the first byte of the output frame
  74. * @param poutbuf_size will contain the length of the output frame
  75. * @return the number of bytes of the input bitstream used
  76. *
  77. * Example:
  78. * @code
  79. * while(in_len){
  80. * len = av_parser_parse(myparser, AVCodecContext, &data, &size,
  81. * in_data, in_len,
  82. * pts, dts);
  83. * in_data += len;
  84. * in_len -= len;
  85. *
  86. * if(size)
  87. * decode_frame(data, size);
  88. * }
  89. * @endcode
  90. */
  91. int av_parser_parse(AVCodecParserContext *s,
  92. AVCodecContext *avctx,
  93. uint8_t **poutbuf, int *poutbuf_size,
  94. const uint8_t *buf, int buf_size,
  95. int64_t pts, int64_t dts)
  96. {
  97. int index, i, k;
  98. uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
  99. if (buf_size == 0) {
  100. /* padding is always necessary even if EOF, so we add it here */
  101. memset(dummy_buf, 0, sizeof(dummy_buf));
  102. buf = dummy_buf;
  103. } else {
  104. /* add a new packet descriptor */
  105. k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  106. s->cur_frame_start_index = k;
  107. s->cur_frame_offset[k] = s->cur_offset;
  108. s->cur_frame_pts[k] = pts;
  109. s->cur_frame_dts[k] = dts;
  110. /* fill first PTS/DTS */
  111. if (s->fetch_timestamp){
  112. s->fetch_timestamp=0;
  113. s->last_pts = pts;
  114. s->last_dts = dts;
  115. s->last_offset = 0;
  116. s->cur_frame_pts[k] =
  117. s->cur_frame_dts[k] = AV_NOPTS_VALUE;
  118. }
  119. }
  120. /* WARNING: the returned index can be negative */
  121. index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
  122. //av_log(NULL, AV_LOG_DEBUG, "parser: in:%"PRId64", %"PRId64", out:%"PRId64", %"PRId64", in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
  123. /* update the file pointer */
  124. if (*poutbuf_size) {
  125. /* fill the data for the current frame */
  126. s->frame_offset = s->last_frame_offset;
  127. s->pts = s->last_pts;
  128. s->dts = s->last_dts;
  129. s->offset = s->last_offset;
  130. /* offset of the next frame */
  131. s->last_frame_offset = s->cur_offset + index;
  132. /* find the packet in which the new frame starts. It
  133. is tricky because of MPEG video start codes
  134. which can begin in one packet and finish in
  135. another packet. In the worst case, an MPEG
  136. video start code could be in 4 different
  137. packets. */
  138. k = s->cur_frame_start_index;
  139. for(i = 0; i < AV_PARSER_PTS_NB; i++) {
  140. if (s->last_frame_offset >= s->cur_frame_offset[k])
  141. break;
  142. k = (k - 1) & (AV_PARSER_PTS_NB - 1);
  143. }
  144. s->last_pts = s->cur_frame_pts[k];
  145. s->last_dts = s->cur_frame_dts[k];
  146. s->last_offset = s->last_frame_offset - s->cur_frame_offset[k];
  147. /* some parsers tell us the packet size even before seeing the first byte of the next packet,
  148. so the next pts/dts is in the next chunk */
  149. if(index == buf_size){
  150. s->fetch_timestamp=1;
  151. }
  152. }
  153. if (index < 0)
  154. index = 0;
  155. s->cur_offset += index;
  156. return index;
  157. }
  158. /**
  159. *
  160. * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
  161. * @deprecated use AVBitstreamFilter
  162. */
  163. int av_parser_change(AVCodecParserContext *s,
  164. AVCodecContext *avctx,
  165. uint8_t **poutbuf, int *poutbuf_size,
  166. const uint8_t *buf, int buf_size, int keyframe){
  167. if(s && s->parser->split){
  168. if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
  169. int i= s->parser->split(avctx, buf, buf_size);
  170. buf += i;
  171. buf_size -= i;
  172. }
  173. }
  174. /* cast to avoid warning about discarding qualifiers */
  175. *poutbuf= (uint8_t *) buf;
  176. *poutbuf_size= buf_size;
  177. if(avctx->extradata){
  178. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
  179. /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
  180. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  181. int size= buf_size + avctx->extradata_size;
  182. *poutbuf_size= size;
  183. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  184. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  185. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  186. return 1;
  187. }
  188. }
  189. return 0;
  190. }
  191. void av_parser_close(AVCodecParserContext *s)
  192. {
  193. if (s->parser->parser_close)
  194. s->parser->parser_close(s);
  195. av_free(s->priv_data);
  196. av_free(s);
  197. }
  198. /*****************************************************/
  199. /**
  200. * combines the (truncated) bitstream to a complete frame
  201. * @returns -1 if no complete frame could be created
  202. */
  203. int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
  204. {
  205. #if 0
  206. if(pc->overread){
  207. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  208. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  209. }
  210. #endif
  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. }
  215. /* flush remaining if EOF */
  216. if(!*buf_size && next == END_NOT_FOUND){
  217. next= 0;
  218. }
  219. pc->last_index= pc->index;
  220. /* copy into buffer end return */
  221. if(next == END_NOT_FOUND){
  222. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  223. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  224. pc->index += *buf_size;
  225. return -1;
  226. }
  227. *buf_size=
  228. pc->overread_index= pc->index + next;
  229. /* append to buffer */
  230. if(pc->index){
  231. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  232. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  233. pc->index = 0;
  234. *buf= pc->buffer;
  235. }
  236. /* store overread bytes */
  237. for(;next < 0; next++){
  238. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  239. pc->overread++;
  240. }
  241. #if 0
  242. if(pc->overread){
  243. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  244. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  245. }
  246. #endif
  247. return 0;
  248. }
  249. void ff_parse_close(AVCodecParserContext *s)
  250. {
  251. ParseContext *pc = s->priv_data;
  252. av_free(pc->buffer);
  253. }
  254. void ff_parse1_close(AVCodecParserContext *s)
  255. {
  256. ParseContext1 *pc1 = s->priv_data;
  257. av_free(pc1->pc.buffer);
  258. av_free(pc1->enc);
  259. }
  260. /*************************/
  261. int ff_mpeg4video_split(AVCodecContext *avctx,
  262. const uint8_t *buf, int buf_size)
  263. {
  264. int i;
  265. uint32_t state= -1;
  266. for(i=0; i<buf_size; i++){
  267. state= (state<<8) | buf[i];
  268. if(state == 0x1B3 || state == 0x1B6)
  269. return i-3;
  270. }
  271. return 0;
  272. }