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.

168 lines
4.6KB

  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. #ifdef HAVE_FAST_UNALIGNED
  42. # ifdef HAVE_FAST_64BIT
  43. while(i<buf_size && !((~*(uint64_t*)(buf+i) & (*(uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
  44. i+=8;
  45. # else
  46. while(i<buf_size && !((~*(uint32_t*)(buf+i) & (*(uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
  47. i+=4;
  48. # endif
  49. #endif
  50. for(; i<buf_size; i++){
  51. if(!buf[i]){
  52. state=2;
  53. break;
  54. }
  55. }
  56. }else if(state<=2){
  57. if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
  58. else if(buf[i]) state = 7;
  59. else state>>=1; //2->1, 1->0, 0->0
  60. }else if(state<=5){
  61. int v= buf[i] & 0x1F;
  62. if(v==7 || v==8 || v==9){
  63. if(pc->frame_start_found){
  64. i++;
  65. goto found;
  66. }
  67. }else if(v==1 || v==2 || v==5){
  68. if(pc->frame_start_found){
  69. state+=8;
  70. continue;
  71. }else
  72. pc->frame_start_found = 1;
  73. }
  74. state= 7;
  75. }else{
  76. if(buf[i] & 0x80)
  77. goto found;
  78. state= 7;
  79. }
  80. }
  81. pc->state= state;
  82. return END_NOT_FOUND;
  83. found:
  84. pc->state=7;
  85. pc->frame_start_found= 0;
  86. return i-(state&5);
  87. }
  88. static int h264_parse(AVCodecParserContext *s,
  89. AVCodecContext *avctx,
  90. const uint8_t **poutbuf, int *poutbuf_size,
  91. const uint8_t *buf, int buf_size)
  92. {
  93. H264Context *h = s->priv_data;
  94. ParseContext *pc = &h->s.parse_context;
  95. int next;
  96. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  97. next= buf_size;
  98. }else{
  99. next= ff_h264_find_frame_end(h, buf, buf_size);
  100. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  101. *poutbuf = NULL;
  102. *poutbuf_size = 0;
  103. return buf_size;
  104. }
  105. if(next<0 && next != END_NOT_FOUND){
  106. assert(pc->last_index + next >= 0 );
  107. ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  108. }
  109. }
  110. *poutbuf = buf;
  111. *poutbuf_size = buf_size;
  112. return next;
  113. }
  114. static int h264_split(AVCodecContext *avctx,
  115. const uint8_t *buf, int buf_size)
  116. {
  117. int i;
  118. uint32_t state = -1;
  119. int has_sps= 0;
  120. for(i=0; i<=buf_size; i++){
  121. if((state&0xFFFFFF1F) == 0x107)
  122. has_sps=1;
  123. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  124. }*/
  125. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  126. if(has_sps){
  127. while(i>4 && buf[i-5]==0) i--;
  128. return i-4;
  129. }
  130. }
  131. if (i<buf_size)
  132. state= (state<<8) | buf[i];
  133. }
  134. return 0;
  135. }
  136. static void close(AVCodecParserContext *s)
  137. {
  138. H264Context *h = s->priv_data;
  139. ParseContext *pc = &h->s.parse_context;
  140. av_free(pc->buffer);
  141. }
  142. AVCodecParser h264_parser = {
  143. { CODEC_ID_H264 },
  144. sizeof(H264Context),
  145. NULL,
  146. h264_parse,
  147. close,
  148. h264_split,
  149. };