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.

149 lines
4.1KB

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