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.

353 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 "libavutil/attributes.h"
  27. #include "parser.h"
  28. #include "h264data.h"
  29. #include "golomb.h"
  30. #include "internal.h"
  31. #include <assert.h>
  32. static int h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
  33. {
  34. int i;
  35. uint32_t state;
  36. ParseContext *pc = &h->parse_context;
  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==6 || 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 = -1;
  110. const uint8_t *ptr;
  111. /* set some sane default values */
  112. s->pict_type = AV_PICTURE_TYPE_I;
  113. s->key_frame = 0;
  114. h->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. if (!buf_size)
  120. return 0;
  121. for(;;) {
  122. int src_length, dst_length, consumed;
  123. buf = avpriv_find_start_code(buf, buf_end, &state);
  124. if(buf >= buf_end)
  125. break;
  126. --buf;
  127. src_length = buf_end - buf;
  128. switch (state & 0x1f) {
  129. case NAL_SLICE:
  130. case NAL_IDR_SLICE:
  131. // Do not walk the whole buffer just to decode slice header
  132. if (src_length > 20)
  133. src_length = 20;
  134. break;
  135. }
  136. ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  137. if (ptr==NULL || dst_length < 0)
  138. break;
  139. init_get_bits(&h->gb, ptr, 8*dst_length);
  140. switch(h->nal_unit_type) {
  141. case NAL_SPS:
  142. ff_h264_decode_seq_parameter_set(h);
  143. break;
  144. case NAL_PPS:
  145. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  146. break;
  147. case NAL_SEI:
  148. ff_h264_decode_sei(h);
  149. break;
  150. case NAL_IDR_SLICE:
  151. s->key_frame = 1;
  152. /* fall through */
  153. case NAL_SLICE:
  154. get_ue_golomb(&h->gb); // skip first_mb_in_slice
  155. slice_type = get_ue_golomb_31(&h->gb);
  156. s->pict_type = golomb_to_pict_type[slice_type % 5];
  157. if (h->sei_recovery_frame_cnt >= 0) {
  158. /* key frame, since recovery_frame_cnt is set */
  159. s->key_frame = 1;
  160. }
  161. pps_id= get_ue_golomb(&h->gb);
  162. if(pps_id>=MAX_PPS_COUNT) {
  163. av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\n");
  164. return -1;
  165. }
  166. if(!h->pps_buffers[pps_id]) {
  167. av_log(h->avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
  168. return -1;
  169. }
  170. h->pps= *h->pps_buffers[pps_id];
  171. if(!h->sps_buffers[h->pps.sps_id]) {
  172. av_log(h->avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
  173. return -1;
  174. }
  175. h->sps = *h->sps_buffers[h->pps.sps_id];
  176. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  177. avctx->profile = ff_h264_get_profile(&h->sps);
  178. avctx->level = h->sps.level_idc;
  179. if(h->sps.frame_mbs_only_flag){
  180. h->picture_structure= PICT_FRAME;
  181. }else{
  182. if(get_bits1(&h->gb)) { //field_pic_flag
  183. h->picture_structure= PICT_TOP_FIELD + get_bits1(&h->gb); //bottom_field_flag
  184. } else {
  185. h->picture_structure= PICT_FRAME;
  186. }
  187. }
  188. if(h->sps.pic_struct_present_flag) {
  189. switch (h->sei_pic_struct) {
  190. case SEI_PIC_STRUCT_TOP_FIELD:
  191. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  192. s->repeat_pict = 0;
  193. break;
  194. case SEI_PIC_STRUCT_FRAME:
  195. case SEI_PIC_STRUCT_TOP_BOTTOM:
  196. case SEI_PIC_STRUCT_BOTTOM_TOP:
  197. s->repeat_pict = 1;
  198. break;
  199. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  200. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  201. s->repeat_pict = 2;
  202. break;
  203. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  204. s->repeat_pict = 3;
  205. break;
  206. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  207. s->repeat_pict = 5;
  208. break;
  209. default:
  210. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  211. break;
  212. }
  213. } else {
  214. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  215. }
  216. return 0; /* no need to evaluate the rest */
  217. }
  218. buf += consumed;
  219. }
  220. /* didn't find a picture! */
  221. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  222. return -1;
  223. }
  224. static int h264_parse(AVCodecParserContext *s,
  225. AVCodecContext *avctx,
  226. const uint8_t **poutbuf, int *poutbuf_size,
  227. const uint8_t *buf, int buf_size)
  228. {
  229. H264Context *h = s->priv_data;
  230. ParseContext *pc = &h->parse_context;
  231. int next;
  232. if (!h->got_first) {
  233. h->got_first = 1;
  234. if (avctx->extradata_size) {
  235. h->avctx = avctx;
  236. // must be done like in the decoder.
  237. // otherwise opening the parser, creating extradata,
  238. // and then closing and opening again
  239. // will cause has_b_frames to be always set.
  240. // NB: estimate_timings_from_pts behaves exactly like this.
  241. if (!avctx->has_b_frames)
  242. h->low_delay = 1;
  243. ff_h264_decode_extradata(h);
  244. }
  245. }
  246. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  247. next= buf_size;
  248. }else{
  249. next = h264_find_frame_end(h, buf, buf_size);
  250. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  251. *poutbuf = NULL;
  252. *poutbuf_size = 0;
  253. return buf_size;
  254. }
  255. if(next<0 && next != END_NOT_FOUND){
  256. assert(pc->last_index + next >= 0 );
  257. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  258. }
  259. }
  260. parse_nal_units(s, avctx, buf, buf_size);
  261. if (h->sei_cpb_removal_delay >= 0) {
  262. s->dts_sync_point = h->sei_buffering_period_present;
  263. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  264. s->pts_dts_delta = h->sei_dpb_output_delay;
  265. } else {
  266. s->dts_sync_point = INT_MIN;
  267. s->dts_ref_dts_delta = INT_MIN;
  268. s->pts_dts_delta = INT_MIN;
  269. }
  270. if (s->flags & PARSER_FLAG_ONCE) {
  271. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  272. }
  273. *poutbuf = buf;
  274. *poutbuf_size = buf_size;
  275. return next;
  276. }
  277. static int h264_split(AVCodecContext *avctx,
  278. const uint8_t *buf, int buf_size)
  279. {
  280. int i;
  281. uint32_t state = -1;
  282. int has_sps= 0;
  283. for(i=0; i<=buf_size; i++){
  284. if((state&0xFFFFFF1F) == 0x107)
  285. has_sps=1;
  286. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  287. }*/
  288. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  289. if(has_sps){
  290. while(i>4 && buf[i-5]==0) i--;
  291. return i-4;
  292. }
  293. }
  294. if (i<buf_size)
  295. state= (state<<8) | buf[i];
  296. }
  297. return 0;
  298. }
  299. static void close(AVCodecParserContext *s)
  300. {
  301. H264Context *h = s->priv_data;
  302. ParseContext *pc = &h->parse_context;
  303. av_free(pc->buffer);
  304. ff_h264_free_context(h);
  305. }
  306. static av_cold int init(AVCodecParserContext *s)
  307. {
  308. H264Context *h = s->priv_data;
  309. h->thread_context[0] = h;
  310. h->slice_context_count = 1;
  311. return 0;
  312. }
  313. AVCodecParser ff_h264_parser = {
  314. .codec_ids = { AV_CODEC_ID_H264 },
  315. .priv_data_size = sizeof(H264Context),
  316. .parser_init = init,
  317. .parser_parse = h264_parse,
  318. .parser_close = close,
  319. .split = h264_split,
  320. };