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.

171 lines
4.8KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... parser
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  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 h264_parser.c
  23. * H.264 / AVC / MPEG4 part10 parser.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "parser.h"
  27. #include "h264_parser.h"
  28. #include <assert.h>
  29. int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
  30. {
  31. int i;
  32. uint32_t state;
  33. ParseContext *pc = &(h->s.parse_context);
  34. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  35. // mb_addr= pc->mb_addr - 1;
  36. state= pc->state;
  37. if(state>13)
  38. state= 7;
  39. for(i=0; i<buf_size; i++){
  40. if(state==7){
  41. #if HAVE_FAST_UNALIGNED
  42. /* we check i<buf_size instead of i+3/7 because its simpler
  43. * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
  44. */
  45. # if HAVE_FAST_64BIT
  46. while(i<buf_size && !((~*(uint64_t*)(buf+i) & (*(uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
  47. i+=8;
  48. # else
  49. while(i<buf_size && !((~*(uint32_t*)(buf+i) & (*(uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
  50. i+=4;
  51. # endif
  52. #endif
  53. for(; i<buf_size; i++){
  54. if(!buf[i]){
  55. state=2;
  56. break;
  57. }
  58. }
  59. }else if(state<=2){
  60. if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
  61. else if(buf[i]) state = 7;
  62. else state>>=1; //2->1, 1->0, 0->0
  63. }else if(state<=5){
  64. int v= buf[i] & 0x1F;
  65. if(v==7 || v==8 || v==9){
  66. if(pc->frame_start_found){
  67. i++;
  68. goto found;
  69. }
  70. }else if(v==1 || v==2 || v==5){
  71. if(pc->frame_start_found){
  72. state+=8;
  73. continue;
  74. }else
  75. pc->frame_start_found = 1;
  76. }
  77. state= 7;
  78. }else{
  79. if(buf[i] & 0x80)
  80. goto found;
  81. state= 7;
  82. }
  83. }
  84. pc->state= state;
  85. return END_NOT_FOUND;
  86. found:
  87. pc->state=7;
  88. pc->frame_start_found= 0;
  89. return i-(state&5);
  90. }
  91. static int h264_parse(AVCodecParserContext *s,
  92. AVCodecContext *avctx,
  93. const uint8_t **poutbuf, int *poutbuf_size,
  94. const uint8_t *buf, int buf_size)
  95. {
  96. H264Context *h = s->priv_data;
  97. ParseContext *pc = &h->s.parse_context;
  98. int next;
  99. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  100. next= buf_size;
  101. }else{
  102. next= ff_h264_find_frame_end(h, buf, buf_size);
  103. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  104. *poutbuf = NULL;
  105. *poutbuf_size = 0;
  106. return buf_size;
  107. }
  108. if(next<0 && next != END_NOT_FOUND){
  109. assert(pc->last_index + next >= 0 );
  110. ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  111. }
  112. }
  113. *poutbuf = buf;
  114. *poutbuf_size = buf_size;
  115. return next;
  116. }
  117. static int h264_split(AVCodecContext *avctx,
  118. const uint8_t *buf, int buf_size)
  119. {
  120. int i;
  121. uint32_t state = -1;
  122. int has_sps= 0;
  123. for(i=0; i<=buf_size; i++){
  124. if((state&0xFFFFFF1F) == 0x107)
  125. has_sps=1;
  126. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  127. }*/
  128. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  129. if(has_sps){
  130. while(i>4 && buf[i-5]==0) i--;
  131. return i-4;
  132. }
  133. }
  134. if (i<buf_size)
  135. state= (state<<8) | buf[i];
  136. }
  137. return 0;
  138. }
  139. static void close(AVCodecParserContext *s)
  140. {
  141. H264Context *h = s->priv_data;
  142. ParseContext *pc = &h->s.parse_context;
  143. av_free(pc->buffer);
  144. }
  145. AVCodecParser h264_parser = {
  146. { CODEC_ID_H264 },
  147. sizeof(H264Context),
  148. NULL,
  149. h264_parse,
  150. close,
  151. h264_split,
  152. };