vc1_parser.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 libavcodec/vc1_parser.c
  24. * VC-1 and WMV3 parser
  25. */
  26. #include "parser.h"
  27. #include "vc1.h"
  28. /**
  29. * finds the end of the current frame in the bitstream.
  30. * @return the position of the first byte of the next frame, or -1
  31. */
  32. static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
  33. int buf_size) {
  34. int pic_found, i;
  35. uint32_t state;
  36. pic_found= pc->frame_start_found;
  37. state= pc->state;
  38. i=0;
  39. if(!pic_found){
  40. for(i=0; i<buf_size; i++){
  41. state= (state<<8) | buf[i];
  42. if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
  43. i++;
  44. pic_found=1;
  45. break;
  46. }
  47. }
  48. }
  49. if(pic_found){
  50. /* EOF considered as end of frame */
  51. if (buf_size == 0)
  52. return 0;
  53. for(; i<buf_size; i++){
  54. state= (state<<8) | buf[i];
  55. if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
  56. pc->frame_start_found=0;
  57. pc->state=-1;
  58. return i-3;
  59. }
  60. }
  61. }
  62. pc->frame_start_found= pic_found;
  63. pc->state= state;
  64. return END_NOT_FOUND;
  65. }
  66. static int vc1_parse(AVCodecParserContext *s,
  67. AVCodecContext *avctx,
  68. const uint8_t **poutbuf, int *poutbuf_size,
  69. const uint8_t *buf, int buf_size)
  70. {
  71. ParseContext *pc = s->priv_data;
  72. int next;
  73. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  74. next= buf_size;
  75. }else{
  76. next= vc1_find_frame_end(pc, buf, buf_size);
  77. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  78. *poutbuf = NULL;
  79. *poutbuf_size = 0;
  80. return buf_size;
  81. }
  82. }
  83. *poutbuf = buf;
  84. *poutbuf_size = buf_size;
  85. return next;
  86. }
  87. static int vc1_split(AVCodecContext *avctx,
  88. const uint8_t *buf, int buf_size)
  89. {
  90. int i;
  91. uint32_t state= -1;
  92. int charged=0;
  93. for(i=0; i<buf_size; i++){
  94. state= (state<<8) | buf[i];
  95. if(IS_MARKER(state)){
  96. if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
  97. charged=1;
  98. }else if(charged){
  99. return i-3;
  100. }
  101. }
  102. }
  103. return 0;
  104. }
  105. AVCodecParser vc1_parser = {
  106. { CODEC_ID_VC1 },
  107. sizeof(ParseContext1),
  108. NULL,
  109. vc1_parse,
  110. ff_parse1_close,
  111. vc1_split,
  112. };