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.

352 lines
11KB

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