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.

410 lines
13KB

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