h264_parser.c 12 KB

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