h264_parser.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 libavcodec/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 "h264data.h"
  29. #include "golomb.h"
  30. #include <assert.h>
  31. int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
  32. {
  33. int i;
  34. uint32_t state;
  35. ParseContext *pc = &(h->s.parse_context);
  36. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  37. // mb_addr= pc->mb_addr - 1;
  38. state= pc->state;
  39. if(state>13)
  40. state= 7;
  41. for(i=0; i<buf_size; i++){
  42. if(state==7){
  43. #if HAVE_FAST_UNALIGNED
  44. /* we check i<buf_size instead of i+3/7 because its simpler
  45. * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
  46. */
  47. # if HAVE_FAST_64BIT
  48. while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
  49. i+=8;
  50. # else
  51. while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
  52. i+=4;
  53. # endif
  54. #endif
  55. for(; i<buf_size; i++){
  56. if(!buf[i]){
  57. state=2;
  58. break;
  59. }
  60. }
  61. }else if(state<=2){
  62. if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
  63. else if(buf[i]) state = 7;
  64. else state>>=1; //2->1, 1->0, 0->0
  65. }else if(state<=5){
  66. int v= buf[i] & 0x1F;
  67. if(v==7 || v==8 || v==9){
  68. if(pc->frame_start_found){
  69. i++;
  70. goto found;
  71. }
  72. }else if(v==1 || v==2 || v==5){
  73. if(pc->frame_start_found){
  74. state+=8;
  75. continue;
  76. }else
  77. pc->frame_start_found = 1;
  78. }
  79. state= 7;
  80. }else{
  81. if(buf[i] & 0x80)
  82. goto found;
  83. state= 7;
  84. }
  85. }
  86. pc->state= state;
  87. return END_NOT_FOUND;
  88. found:
  89. pc->state=7;
  90. pc->frame_start_found= 0;
  91. return i-(state&5);
  92. }
  93. /*!
  94. * Parse NAL units of found picture and decode some basic information.
  95. *
  96. * @param s parser context.
  97. * @param avctx codec context.
  98. * @param buf buffer with field/frame data.
  99. * @param buf_size size of the buffer.
  100. */
  101. static inline int parse_nal_units(AVCodecParserContext *s,
  102. AVCodecContext *avctx,
  103. const uint8_t *buf, int buf_size)
  104. {
  105. H264Context *h = s->priv_data;
  106. const uint8_t *buf_end = buf + buf_size;
  107. unsigned int pps_id;
  108. unsigned int slice_type;
  109. int state;
  110. const uint8_t *ptr;
  111. /* set some sane default values */
  112. s->pict_type = FF_I_TYPE;
  113. s->key_frame = 0;
  114. h->s.avctx= avctx;
  115. h->sei_recovery_frame_cnt = -1;
  116. h->sei_dpb_output_delay = 0;
  117. h->sei_cpb_removal_delay = -1;
  118. h->sei_buffering_period_present = 0;
  119. for(;;) {
  120. int src_length, dst_length, consumed;
  121. buf = ff_find_start_code(buf, buf_end, &state);
  122. if(buf >= buf_end)
  123. break;
  124. --buf;
  125. src_length = buf_end - buf;
  126. switch (state & 0x1f) {
  127. case NAL_SLICE:
  128. case NAL_IDR_SLICE:
  129. // Do not walk the whole buffer just to decode slice header
  130. if (src_length > 20)
  131. src_length = 20;
  132. break;
  133. }
  134. ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  135. if (ptr==NULL || dst_length < 0)
  136. break;
  137. init_get_bits(&h->s.gb, ptr, 8*dst_length);
  138. switch(h->nal_unit_type) {
  139. case NAL_SPS:
  140. ff_h264_decode_seq_parameter_set(h);
  141. break;
  142. case NAL_PPS:
  143. ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
  144. break;
  145. case NAL_SEI:
  146. ff_h264_decode_sei(h);
  147. break;
  148. case NAL_IDR_SLICE:
  149. s->key_frame = 1;
  150. /* fall through */
  151. case NAL_SLICE:
  152. get_ue_golomb(&h->s.gb); // skip first_mb_in_slice
  153. slice_type = get_ue_golomb_31(&h->s.gb);
  154. s->pict_type = golomb_to_pict_type[slice_type % 5];
  155. if (h->sei_recovery_frame_cnt >= 0) {
  156. /* key frame, since recovery_frame_cnt is set */
  157. s->key_frame = 1;
  158. }
  159. pps_id= get_ue_golomb(&h->s.gb);
  160. if(pps_id>=MAX_PPS_COUNT) {
  161. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  162. return -1;
  163. }
  164. if(!h->pps_buffers[pps_id]) {
  165. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
  166. return -1;
  167. }
  168. h->pps= *h->pps_buffers[pps_id];
  169. if(!h->sps_buffers[h->pps.sps_id]) {
  170. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
  171. return -1;
  172. }
  173. h->sps = *h->sps_buffers[h->pps.sps_id];
  174. h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
  175. if(h->sps.frame_mbs_only_flag){
  176. h->s.picture_structure= PICT_FRAME;
  177. }else{
  178. if(get_bits1(&h->s.gb)) { //field_pic_flag
  179. h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
  180. } else {
  181. h->s.picture_structure= PICT_FRAME;
  182. }
  183. }
  184. if(h->sps.pic_struct_present_flag) {
  185. switch (h->sei_pic_struct) {
  186. case SEI_PIC_STRUCT_TOP_FIELD:
  187. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  188. s->repeat_pict = 0;
  189. break;
  190. case SEI_PIC_STRUCT_FRAME:
  191. case SEI_PIC_STRUCT_TOP_BOTTOM:
  192. case SEI_PIC_STRUCT_BOTTOM_TOP:
  193. s->repeat_pict = 1;
  194. break;
  195. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  196. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  197. s->repeat_pict = 2;
  198. break;
  199. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  200. s->repeat_pict = 3;
  201. break;
  202. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  203. s->repeat_pict = 5;
  204. break;
  205. default:
  206. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  207. break;
  208. }
  209. } else {
  210. s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
  211. }
  212. return 0; /* no need to evaluate the rest */
  213. }
  214. buf += consumed;
  215. }
  216. /* didn't find a picture! */
  217. av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  218. return -1;
  219. }
  220. static int h264_parse(AVCodecParserContext *s,
  221. AVCodecContext *avctx,
  222. const uint8_t **poutbuf, int *poutbuf_size,
  223. const uint8_t *buf, int buf_size)
  224. {
  225. H264Context *h = s->priv_data;
  226. ParseContext *pc = &h->s.parse_context;
  227. int next;
  228. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  229. next= buf_size;
  230. }else{
  231. next= ff_h264_find_frame_end(h, buf, buf_size);
  232. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  233. *poutbuf = NULL;
  234. *poutbuf_size = 0;
  235. return buf_size;
  236. }
  237. if(next<0 && next != END_NOT_FOUND){
  238. assert(pc->last_index + next >= 0 );
  239. ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  240. }
  241. parse_nal_units(s, avctx, buf, buf_size);
  242. if (h->sei_cpb_removal_delay >= 0) {
  243. s->dts_sync_point = h->sei_buffering_period_present;
  244. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  245. s->pts_dts_delta = h->sei_dpb_output_delay;
  246. } else {
  247. s->dts_sync_point = INT_MIN;
  248. s->dts_ref_dts_delta = INT_MIN;
  249. s->pts_dts_delta = INT_MIN;
  250. }
  251. }
  252. *poutbuf = buf;
  253. *poutbuf_size = buf_size;
  254. return next;
  255. }
  256. static int h264_split(AVCodecContext *avctx,
  257. const uint8_t *buf, int buf_size)
  258. {
  259. int i;
  260. uint32_t state = -1;
  261. int has_sps= 0;
  262. for(i=0; i<=buf_size; i++){
  263. if((state&0xFFFFFF1F) == 0x107)
  264. has_sps=1;
  265. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  266. }*/
  267. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  268. if(has_sps){
  269. while(i>4 && buf[i-5]==0) i--;
  270. return i-4;
  271. }
  272. }
  273. if (i<buf_size)
  274. state= (state<<8) | buf[i];
  275. }
  276. return 0;
  277. }
  278. static void close(AVCodecParserContext *s)
  279. {
  280. H264Context *h = s->priv_data;
  281. ParseContext *pc = &h->s.parse_context;
  282. av_free(pc->buffer);
  283. }
  284. AVCodecParser h264_parser = {
  285. { CODEC_ID_H264 },
  286. sizeof(H264Context),
  287. NULL,
  288. h264_parse,
  289. close,
  290. h264_split,
  291. };