cavs_parser.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Chinese AVS video (AVS1-P2, JiZhun profile) parser.
  3. * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/cavs_parser.c
  23. * Chinese AVS video (AVS1-P2, JiZhun profile) parser
  24. * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  25. */
  26. #include "parser.h"
  27. #include "cavs.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 cavs_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 == PIC_I_START_CODE || state == PIC_PB_START_CODE){
  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((state&0xFFFFFF00) == 0x100){
  56. if(state > SLICE_MAX_START_CODE){
  57. pc->frame_start_found=0;
  58. pc->state=-1;
  59. return i-3;
  60. }
  61. }
  62. }
  63. }
  64. pc->frame_start_found= pic_found;
  65. pc->state= state;
  66. return END_NOT_FOUND;
  67. }
  68. static int cavsvideo_parse(AVCodecParserContext *s,
  69. AVCodecContext *avctx,
  70. const uint8_t **poutbuf, int *poutbuf_size,
  71. const uint8_t *buf, int buf_size)
  72. {
  73. ParseContext *pc = s->priv_data;
  74. int next;
  75. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  76. next= buf_size;
  77. }else{
  78. next= cavs_find_frame_end(pc, buf, buf_size);
  79. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  80. *poutbuf = NULL;
  81. *poutbuf_size = 0;
  82. return buf_size;
  83. }
  84. }
  85. *poutbuf = buf;
  86. *poutbuf_size = buf_size;
  87. return next;
  88. }
  89. AVCodecParser cavsvideo_parser = {
  90. { CODEC_ID_CAVS },
  91. sizeof(ParseContext1),
  92. NULL,
  93. cavsvideo_parse,
  94. ff_parse1_close,
  95. ff_mpeg4video_split,
  96. };