PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/amffmpeg/libavcodec/vc1_parser.c

https://github.com/J1nx-Hackable-Gadgets/libamplayer-m3
C | 180 lines | 127 code | 22 blank | 31 comment | 30 complexity | 675db4068202bb2a996984b32cabd22d MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-3.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. /*
  2. * VC-1 and WMV3 parser
  3. * Copyright (c) 2006-2007 Konstantin Shishkov
  4. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, 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. /**
  23. * @file
  24. * VC-1 and WMV3 parser
  25. */
  26. #include "parser.h"
  27. #include "vc1.h"
  28. #include "get_bits.h"
  29. typedef struct {
  30. ParseContext pc;
  31. VC1Context v;
  32. } VC1ParseContext;
  33. static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
  34. const uint8_t *buf, int buf_size)
  35. {
  36. VC1ParseContext *vpc = s->priv_data;
  37. GetBitContext gb;
  38. const uint8_t *start, *end, *next;
  39. uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  40. vpc->v.s.avctx = avctx;
  41. vpc->v.parse_only = 1;
  42. next = buf;
  43. for(start = buf, end = buf + buf_size; next < end; start = next){
  44. int buf2_size, size;
  45. next = find_next_marker(start + 4, end);
  46. size = next - start - 4;
  47. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  48. init_get_bits(&gb, buf2, buf2_size * 8);
  49. if(size <= 0) continue;
  50. switch(AV_RB32(start)){
  51. case VC1_CODE_SEQHDR:
  52. vc1_decode_sequence_header(avctx, &vpc->v, &gb);
  53. break;
  54. case VC1_CODE_ENTRYPOINT:
  55. vc1_decode_entry_point(avctx, &vpc->v, &gb);
  56. break;
  57. case VC1_CODE_FRAME:
  58. if(vpc->v.profile < PROFILE_ADVANCED)
  59. vc1_parse_frame_header (&vpc->v, &gb);
  60. else
  61. vc1_parse_frame_header_adv(&vpc->v, &gb);
  62. /* keep AV_PICTURE_TYPE_BI internal to VC1 */
  63. if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
  64. s->pict_type = AV_PICTURE_TYPE_B;
  65. else
  66. s->pict_type = vpc->v.s.pict_type;
  67. break;
  68. }
  69. }
  70. avctx->frame_interlace = vpc->v.interlace && vpc->v.fcm;
  71. av_free(buf2);
  72. }
  73. /**
  74. * finds the end of the current frame in the bitstream.
  75. * @return the position of the first byte of the next frame, or -1
  76. */
  77. static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
  78. int buf_size) {
  79. int pic_found, i;
  80. uint32_t state;
  81. pic_found= pc->frame_start_found;
  82. state= pc->state;
  83. i=0;
  84. if(!pic_found){
  85. for(i=0; i<buf_size; i++){
  86. state= (state<<8) | buf[i];
  87. if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
  88. i++;
  89. pic_found=1;
  90. break;
  91. }
  92. }
  93. }
  94. if(pic_found){
  95. /* EOF considered as end of frame */
  96. if (buf_size == 0)
  97. return 0;
  98. for(; i<buf_size; i++){
  99. state= (state<<8) | buf[i];
  100. if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
  101. pc->frame_start_found=0;
  102. pc->state=-1;
  103. return i-3;
  104. }
  105. }
  106. }
  107. pc->frame_start_found= pic_found;
  108. pc->state= state;
  109. return END_NOT_FOUND;
  110. }
  111. static int vc1_parse(AVCodecParserContext *s,
  112. AVCodecContext *avctx,
  113. const uint8_t **poutbuf, int *poutbuf_size,
  114. const uint8_t *buf, int buf_size)
  115. {
  116. VC1ParseContext *vpc = s->priv_data;
  117. int next;
  118. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  119. next= buf_size;
  120. }else{
  121. next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
  122. if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  123. *poutbuf = NULL;
  124. *poutbuf_size = 0;
  125. return buf_size;
  126. }
  127. }
  128. vc1_extract_headers(s, avctx, buf, buf_size);
  129. *poutbuf = buf;
  130. *poutbuf_size = buf_size;
  131. return next;
  132. }
  133. static int vc1_split(AVCodecContext *avctx,
  134. const uint8_t *buf, int buf_size)
  135. {
  136. int i;
  137. uint32_t state= -1;
  138. int charged=0;
  139. for(i=0; i<buf_size; i++){
  140. state= (state<<8) | buf[i];
  141. if(IS_MARKER(state)){
  142. if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
  143. charged=1;
  144. }else if(charged){
  145. return i-3;
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. AVCodecParser ff_vc1_parser = {
  152. { CODEC_ID_VC1 },
  153. sizeof(VC1ParseContext),
  154. NULL,
  155. vc1_parse,
  156. ff_parse1_close,
  157. vc1_split,
  158. };