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.

598 lines
21KB

  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 / MPEG-4 part10 parser.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include <assert.h>
  27. #include <stdint.h>
  28. #include "libavutil/avutil.h"
  29. #include "libavutil/error.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/mem.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "avcodec.h"
  34. #include "get_bits.h"
  35. #include "golomb_legacy.h"
  36. #include "h264.h"
  37. #include "h264_sei.h"
  38. #include "h264_ps.h"
  39. #include "h264data.h"
  40. #include "internal.h"
  41. #include "mpegutils.h"
  42. #include "parser.h"
  43. typedef struct H264ParseContext {
  44. ParseContext pc;
  45. H264ParamSets ps;
  46. H264DSPContext h264dsp;
  47. H264POCContext poc;
  48. H264SEIContext sei;
  49. int is_avc;
  50. int nal_length_size;
  51. int got_first;
  52. int picture_structure;
  53. } H264ParseContext;
  54. static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
  55. int buf_size)
  56. {
  57. int i;
  58. uint32_t state;
  59. ParseContext *pc = &p->pc;
  60. // mb_addr= pc->mb_addr - 1;
  61. state = pc->state;
  62. if (state > 13)
  63. state = 7;
  64. for (i = 0; i < buf_size; i++) {
  65. if (state == 7) {
  66. i += p->h264dsp.startcode_find_candidate(buf + i, buf_size - i);
  67. if (i < buf_size)
  68. state = 2;
  69. } else if (state <= 2) {
  70. if (buf[i] == 1)
  71. state ^= 5; // 2->7, 1->4, 0->5
  72. else if (buf[i])
  73. state = 7;
  74. else
  75. state >>= 1; // 2->1, 1->0, 0->0
  76. } else if (state <= 5) {
  77. int nalu_type = buf[i] & 0x1F;
  78. if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
  79. nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
  80. if (pc->frame_start_found) {
  81. i++;
  82. goto found;
  83. }
  84. } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||
  85. nalu_type == H264_NAL_IDR_SLICE) {
  86. if (pc->frame_start_found) {
  87. state += 8;
  88. continue;
  89. } else
  90. pc->frame_start_found = 1;
  91. }
  92. state = 7;
  93. } else {
  94. // first_mb_in_slice is 0, probably the first nal of a new slice
  95. if (buf[i] & 0x80)
  96. goto found;
  97. state = 7;
  98. }
  99. }
  100. pc->state = state;
  101. return END_NOT_FOUND;
  102. found:
  103. pc->state = 7;
  104. pc->frame_start_found = 0;
  105. return i - (state & 5);
  106. }
  107. static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb,
  108. AVCodecContext *avctx)
  109. {
  110. H264PredWeightTable pwt;
  111. int slice_type_nos = s->pict_type & 3;
  112. H264ParseContext *p = s->priv_data;
  113. int list_count, ref_count[2];
  114. if (p->ps.pps->redundant_pic_cnt_present)
  115. get_ue_golomb(gb); // redundant_pic_count
  116. if (slice_type_nos == AV_PICTURE_TYPE_B)
  117. get_bits1(gb); // direct_spatial_mv_pred
  118. if (ff_h264_parse_ref_count(&list_count, ref_count, gb, p->ps.pps,
  119. slice_type_nos, p->picture_structure) < 0)
  120. return AVERROR_INVALIDDATA;
  121. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  122. int list;
  123. for (list = 0; list < list_count; list++) {
  124. if (get_bits1(gb)) {
  125. int index;
  126. for (index = 0; ; index++) {
  127. unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(gb);
  128. if (reordering_of_pic_nums_idc < 3)
  129. get_ue_golomb(gb);
  130. else if (reordering_of_pic_nums_idc > 3) {
  131. av_log(avctx, AV_LOG_ERROR,
  132. "illegal reordering_of_pic_nums_idc %d\n",
  133. reordering_of_pic_nums_idc);
  134. return AVERROR_INVALIDDATA;
  135. } else
  136. break;
  137. if (index >= ref_count[list]) {
  138. av_log(avctx, AV_LOG_ERROR,
  139. "reference count %d overflow\n", index);
  140. return AVERROR_INVALIDDATA;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
  147. (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
  148. ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
  149. &pwt);
  150. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  151. int i;
  152. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  153. MMCOOpcode opcode = get_ue_golomb_31(gb);
  154. if (opcode > (unsigned) MMCO_LONG) {
  155. av_log(avctx, AV_LOG_ERROR,
  156. "illegal memory management control operation %d\n",
  157. opcode);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. if (opcode == MMCO_END)
  161. return 0;
  162. else if (opcode == MMCO_RESET)
  163. return 1;
  164. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
  165. get_ue_golomb(gb);
  166. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  167. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
  168. get_ue_golomb_31(gb);
  169. }
  170. }
  171. return 0;
  172. }
  173. /**
  174. * Parse NAL units of found picture and decode some basic information.
  175. *
  176. * @param s parser context.
  177. * @param avctx codec context.
  178. * @param buf buffer with field/frame data.
  179. * @param buf_size size of the buffer.
  180. */
  181. static inline int parse_nal_units(AVCodecParserContext *s,
  182. AVCodecContext *avctx,
  183. const uint8_t *buf, int buf_size)
  184. {
  185. H264ParseContext *p = s->priv_data;
  186. const uint8_t *buf_end = buf + buf_size;
  187. H2645NAL nal = { NULL };
  188. unsigned int pps_id;
  189. unsigned int slice_type;
  190. int state = -1, got_reset = 0;
  191. int field_poc[2];
  192. int ret;
  193. /* set some sane default values */
  194. s->pict_type = AV_PICTURE_TYPE_I;
  195. s->key_frame = 0;
  196. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  197. ff_h264_sei_uninit(&p->sei);
  198. if (!buf_size)
  199. return 0;
  200. for (;;) {
  201. const SPS *sps;
  202. int src_length, consumed;
  203. buf = avpriv_find_start_code(buf, buf_end, &state);
  204. if (buf >= buf_end)
  205. break;
  206. --buf;
  207. src_length = buf_end - buf;
  208. switch (state & 0x1f) {
  209. case H264_NAL_SLICE:
  210. case H264_NAL_IDR_SLICE:
  211. // Do not walk the whole buffer just to decode slice header
  212. if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  213. /* IDR or disposable slice
  214. * No need to decode many bytes because MMCOs shall not be present. */
  215. if (src_length > 60)
  216. src_length = 60;
  217. } else {
  218. /* To decode up to MMCOs */
  219. if (src_length > 1000)
  220. src_length = 1000;
  221. }
  222. break;
  223. }
  224. consumed = ff_h2645_extract_rbsp(buf, src_length, &nal);
  225. if (consumed < 0)
  226. break;
  227. ret = init_get_bits(&nal.gb, nal.data, nal.size * 8);
  228. if (ret < 0)
  229. goto fail;
  230. get_bits1(&nal.gb);
  231. nal.ref_idc = get_bits(&nal.gb, 2);
  232. nal.type = get_bits(&nal.gb, 5);
  233. switch (nal.type) {
  234. case H264_NAL_SPS:
  235. ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps);
  236. break;
  237. case H264_NAL_PPS:
  238. ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
  239. nal.size_bits);
  240. break;
  241. case H264_NAL_SEI:
  242. ff_h264_sei_decode(&p->sei, &nal.gb, &p->ps, avctx);
  243. break;
  244. case H264_NAL_IDR_SLICE:
  245. s->key_frame = 1;
  246. p->poc.prev_frame_num = 0;
  247. p->poc.prev_frame_num_offset = 0;
  248. p->poc.prev_poc_msb =
  249. p->poc.prev_poc_lsb = 0;
  250. /* fall through */
  251. case H264_NAL_SLICE:
  252. get_ue_golomb(&nal.gb); // skip first_mb_in_slice
  253. slice_type = get_ue_golomb_31(&nal.gb);
  254. s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
  255. if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
  256. /* key frame, since recovery_frame_cnt is set */
  257. s->key_frame = 1;
  258. }
  259. pps_id = get_ue_golomb(&nal.gb);
  260. if (pps_id >= MAX_PPS_COUNT) {
  261. av_log(avctx, AV_LOG_ERROR,
  262. "pps_id %u out of range\n", pps_id);
  263. goto fail;
  264. }
  265. if (!p->ps.pps_list[pps_id]) {
  266. av_log(avctx, AV_LOG_ERROR,
  267. "non-existing PPS %u referenced\n", pps_id);
  268. goto fail;
  269. }
  270. p->ps.pps = (const PPS*)p->ps.pps_list[pps_id]->data;
  271. if (!p->ps.sps_list[p->ps.pps->sps_id]) {
  272. av_log(avctx, AV_LOG_ERROR,
  273. "non-existing SPS %u referenced\n", p->ps.pps->sps_id);
  274. goto fail;
  275. }
  276. p->ps.sps = (SPS*)p->ps.sps_list[p->ps.pps->sps_id]->data;
  277. sps = p->ps.sps;
  278. p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
  279. s->coded_width = 16 * sps->mb_width;
  280. s->coded_height = 16 * sps->mb_height;
  281. s->width = s->coded_width - (sps->crop_right + sps->crop_left);
  282. s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
  283. if (s->width <= 0 || s->height <= 0) {
  284. s->width = s->coded_width;
  285. s->height = s->coded_height;
  286. }
  287. switch (sps->bit_depth_luma) {
  288. case 9:
  289. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P9;
  290. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P9;
  291. else s->format = AV_PIX_FMT_YUV420P9;
  292. break;
  293. case 10:
  294. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P10;
  295. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P10;
  296. else s->format = AV_PIX_FMT_YUV420P10;
  297. break;
  298. case 8:
  299. if (sps->chroma_format_idc == 3) s->format = AV_PIX_FMT_YUV444P;
  300. else if (sps->chroma_format_idc == 2) s->format = AV_PIX_FMT_YUV422P;
  301. else s->format = AV_PIX_FMT_YUV420P;
  302. break;
  303. default:
  304. s->format = AV_PIX_FMT_NONE;
  305. }
  306. avctx->profile = ff_h264_get_profile(sps);
  307. avctx->level = sps->level_idc;
  308. if (sps->frame_mbs_only_flag) {
  309. p->picture_structure = PICT_FRAME;
  310. } else {
  311. if (get_bits1(&nal.gb)) { // field_pic_flag
  312. p->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
  313. } else {
  314. p->picture_structure = PICT_FRAME;
  315. }
  316. }
  317. if (nal.type == H264_NAL_IDR_SLICE)
  318. get_ue_golomb(&nal.gb); /* idr_pic_id */
  319. if (sps->poc_type == 0) {
  320. p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
  321. if (p->ps.pps->pic_order_present == 1 &&
  322. p->picture_structure == PICT_FRAME)
  323. p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
  324. }
  325. if (sps->poc_type == 1 &&
  326. !sps->delta_pic_order_always_zero_flag) {
  327. p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
  328. if (p->ps.pps->pic_order_present == 1 &&
  329. p->picture_structure == PICT_FRAME)
  330. p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
  331. }
  332. /* Decode POC of this picture.
  333. * The prev_ values needed for decoding POC of the next picture are not set here. */
  334. field_poc[0] = field_poc[1] = INT_MAX;
  335. ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
  336. &p->poc, p->picture_structure, nal.ref_idc);
  337. /* Continue parsing to check if MMCO_RESET is present.
  338. * FIXME: MMCO_RESET could appear in non-first slice.
  339. * Maybe, we should parse all undisposable non-IDR slice of this
  340. * picture until encountering MMCO_RESET in a slice of it. */
  341. if (nal.ref_idc && nal.type != H264_NAL_IDR_SLICE) {
  342. got_reset = scan_mmco_reset(s, &nal.gb, avctx);
  343. if (got_reset < 0)
  344. goto fail;
  345. }
  346. /* Set up the prev_ values for decoding POC of the next picture. */
  347. p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
  348. p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
  349. if (nal.ref_idc != 0) {
  350. if (!got_reset) {
  351. p->poc.prev_poc_msb = p->poc.poc_msb;
  352. p->poc.prev_poc_lsb = p->poc.poc_lsb;
  353. } else {
  354. p->poc.prev_poc_msb = 0;
  355. p->poc.prev_poc_lsb =
  356. p->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  357. }
  358. }
  359. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  360. switch (p->sei.picture_timing.pic_struct) {
  361. case SEI_PIC_STRUCT_TOP_FIELD:
  362. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  363. s->repeat_pict = 0;
  364. break;
  365. case SEI_PIC_STRUCT_FRAME:
  366. case SEI_PIC_STRUCT_TOP_BOTTOM:
  367. case SEI_PIC_STRUCT_BOTTOM_TOP:
  368. s->repeat_pict = 1;
  369. break;
  370. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  371. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  372. s->repeat_pict = 2;
  373. break;
  374. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  375. s->repeat_pict = 3;
  376. break;
  377. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  378. s->repeat_pict = 5;
  379. break;
  380. default:
  381. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  382. break;
  383. }
  384. } else {
  385. s->repeat_pict = p->picture_structure == PICT_FRAME ? 1 : 0;
  386. }
  387. if (p->picture_structure == PICT_FRAME) {
  388. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  389. if (sps->pic_struct_present_flag && p->sei.picture_timing.present) {
  390. switch (p->sei.picture_timing.pic_struct) {
  391. case SEI_PIC_STRUCT_TOP_BOTTOM:
  392. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  393. s->field_order = AV_FIELD_TT;
  394. break;
  395. case SEI_PIC_STRUCT_BOTTOM_TOP:
  396. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  397. s->field_order = AV_FIELD_BB;
  398. break;
  399. default:
  400. s->field_order = AV_FIELD_PROGRESSIVE;
  401. break;
  402. }
  403. } else {
  404. if (field_poc[0] < field_poc[1])
  405. s->field_order = AV_FIELD_TT;
  406. else if (field_poc[0] > field_poc[1])
  407. s->field_order = AV_FIELD_BB;
  408. else
  409. s->field_order = AV_FIELD_PROGRESSIVE;
  410. }
  411. } else {
  412. if (p->picture_structure == PICT_TOP_FIELD)
  413. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  414. else
  415. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  416. s->field_order = AV_FIELD_UNKNOWN;
  417. }
  418. av_freep(&nal.rbsp_buffer);
  419. return 0; /* no need to evaluate the rest */
  420. }
  421. buf += consumed;
  422. }
  423. /* didn't find a picture! */
  424. av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  425. fail:
  426. av_freep(&nal.rbsp_buffer);
  427. return -1;
  428. }
  429. static int h264_parse(AVCodecParserContext *s,
  430. AVCodecContext *avctx,
  431. const uint8_t **poutbuf, int *poutbuf_size,
  432. const uint8_t *buf, int buf_size)
  433. {
  434. H264ParseContext *p = s->priv_data;
  435. ParseContext *pc = &p->pc;
  436. int next;
  437. if (!p->got_first) {
  438. p->got_first = 1;
  439. if (avctx->extradata_size) {
  440. ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  441. &p->ps, &p->is_avc, &p->nal_length_size,
  442. avctx->err_recognition, avctx);
  443. }
  444. }
  445. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  446. next = buf_size;
  447. } else {
  448. next = h264_find_frame_end(p, buf, buf_size);
  449. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  450. *poutbuf = NULL;
  451. *poutbuf_size = 0;
  452. return buf_size;
  453. }
  454. if (next < 0 && next != END_NOT_FOUND) {
  455. assert(pc->last_index + next >= 0);
  456. h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
  457. }
  458. }
  459. parse_nal_units(s, avctx, buf, buf_size);
  460. if (p->sei.picture_timing.cpb_removal_delay >= 0) {
  461. s->dts_sync_point = p->sei.buffering_period.present;
  462. s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
  463. s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
  464. } else {
  465. s->dts_sync_point = INT_MIN;
  466. s->dts_ref_dts_delta = INT_MIN;
  467. s->pts_dts_delta = INT_MIN;
  468. }
  469. if (s->flags & PARSER_FLAG_ONCE) {
  470. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  471. }
  472. *poutbuf = buf;
  473. *poutbuf_size = buf_size;
  474. return next;
  475. }
  476. static int h264_split(AVCodecContext *avctx,
  477. const uint8_t *buf, int buf_size)
  478. {
  479. int i;
  480. uint32_t state = -1;
  481. int has_sps = 0;
  482. for (i = 0; i <= buf_size; i++) {
  483. if ((state & 0xFFFFFF1F) == 0x107)
  484. has_sps = 1;
  485. /* if((state&0xFFFFFF1F) == 0x101 ||
  486. * (state&0xFFFFFF1F) == 0x102 ||
  487. * (state&0xFFFFFF1F) == 0x105) {
  488. * }
  489. */
  490. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
  491. (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
  492. (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
  493. (state & 0xFFFFFF1F) != 0x10f) {
  494. if (has_sps) {
  495. while (i > 4 && buf[i - 5] == 0)
  496. i--;
  497. return i - 4;
  498. }
  499. }
  500. if (i < buf_size)
  501. state = (state << 8) | buf[i];
  502. }
  503. return 0;
  504. }
  505. static void h264_close(AVCodecParserContext *s)
  506. {
  507. H264ParseContext *p = s->priv_data;
  508. ParseContext *pc = &p->pc;
  509. int i;
  510. av_free(pc->buffer);
  511. ff_h264_sei_uninit(&p->sei);
  512. for (i = 0; i < FF_ARRAY_ELEMS(p->ps.sps_list); i++)
  513. av_buffer_unref(&p->ps.sps_list[i]);
  514. for (i = 0; i < FF_ARRAY_ELEMS(p->ps.pps_list); i++)
  515. av_buffer_unref(&p->ps.pps_list[i]);
  516. }
  517. static av_cold int init(AVCodecParserContext *s)
  518. {
  519. H264ParseContext *p = s->priv_data;
  520. ff_h264dsp_init(&p->h264dsp, 8, 1);
  521. return 0;
  522. }
  523. AVCodecParser ff_h264_parser = {
  524. .codec_ids = { AV_CODEC_ID_H264 },
  525. .priv_data_size = sizeof(H264ParseContext),
  526. .parser_init = init,
  527. .parser_parse = h264_parse,
  528. .parser_close = h264_close,
  529. .split = h264_split,
  530. };