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.

159 lines
4.3KB

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