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.

516 lines
17KB

  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 "mpegutils.h"
  32. #include <assert.h>
  33. static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
  34. int buf_size)
  35. {
  36. int i;
  37. uint32_t state;
  38. ParseContext *pc = &h->parse_context;
  39. // mb_addr= pc->mb_addr - 1;
  40. state = pc->state;
  41. if (state > 13)
  42. state = 7;
  43. for (i = 0; i < buf_size; i++) {
  44. if (state == 7) {
  45. i += h->h264dsp.h264_find_start_code_candidate(buf + i, buf_size - i);
  46. if (i < buf_size)
  47. state = 2;
  48. } else if (state <= 2) {
  49. if (buf[i] == 1)
  50. state ^= 5; // 2->7, 1->4, 0->5
  51. else if (buf[i])
  52. state = 7;
  53. else
  54. state >>= 1; // 2->1, 1->0, 0->0
  55. } else if (state <= 5) {
  56. int nalu_type = buf[i] & 0x1F;
  57. if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
  58. nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
  59. if (pc->frame_start_found) {
  60. i++;
  61. goto found;
  62. }
  63. } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
  64. nalu_type == NAL_IDR_SLICE) {
  65. if (pc->frame_start_found) {
  66. state += 8;
  67. continue;
  68. } else
  69. pc->frame_start_found = 1;
  70. }
  71. state = 7;
  72. } else {
  73. if (buf[i] & 0x80)
  74. goto found;
  75. state = 7;
  76. }
  77. }
  78. pc->state = state;
  79. return END_NOT_FOUND;
  80. found:
  81. pc->state = 7;
  82. pc->frame_start_found = 0;
  83. return i - (state & 5);
  84. }
  85. static int scan_mmco_reset(AVCodecParserContext *s)
  86. {
  87. H264Context *h = s->priv_data;
  88. h->slice_type_nos = s->pict_type & 3;
  89. if (h->pps.redundant_pic_cnt_present)
  90. get_ue_golomb(&h->gb); // redundant_pic_count
  91. if (ff_set_ref_count(h) < 0)
  92. return AVERROR_INVALIDDATA;
  93. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  94. int list;
  95. for (list = 0; list < h->list_count; list++) {
  96. if (get_bits1(&h->gb)) {
  97. int index;
  98. for (index = 0; ; index++) {
  99. unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
  100. if (reordering_of_pic_nums_idc < 3)
  101. get_ue_golomb(&h->gb);
  102. else if (reordering_of_pic_nums_idc > 3) {
  103. av_log(h->avctx, AV_LOG_ERROR,
  104. "illegal reordering_of_pic_nums_idc %d\n",
  105. reordering_of_pic_nums_idc);
  106. return AVERROR_INVALIDDATA;
  107. } else
  108. break;
  109. if (index >= h->ref_count[list]) {
  110. av_log(h->avctx, AV_LOG_ERROR,
  111. "reference count %d overflow\n", index);
  112. return AVERROR_INVALIDDATA;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  119. (h->pps.weighted_bipred_idc == 1 && h->slice_type_nos == AV_PICTURE_TYPE_B))
  120. ff_pred_weight_table(h);
  121. if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
  122. int i;
  123. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  124. MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
  125. if (opcode > (unsigned) MMCO_LONG) {
  126. av_log(h->avctx, AV_LOG_ERROR,
  127. "illegal memory management control operation %d\n",
  128. opcode);
  129. return AVERROR_INVALIDDATA;
  130. }
  131. if (opcode == MMCO_END)
  132. return 0;
  133. else if (opcode == MMCO_RESET)
  134. return 1;
  135. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
  136. get_ue_golomb(&h->gb);
  137. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  138. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
  139. get_ue_golomb_31(&h->gb);
  140. }
  141. }
  142. return 0;
  143. }
  144. /**
  145. * Parse NAL units of found picture and decode some basic information.
  146. *
  147. * @param s parser context.
  148. * @param avctx codec context.
  149. * @param buf buffer with field/frame data.
  150. * @param buf_size size of the buffer.
  151. */
  152. static inline int parse_nal_units(AVCodecParserContext *s,
  153. AVCodecContext *avctx,
  154. const uint8_t *buf, int buf_size)
  155. {
  156. H264Context *h = s->priv_data;
  157. const uint8_t *buf_end = buf + buf_size;
  158. unsigned int pps_id;
  159. unsigned int slice_type;
  160. int state = -1, got_reset = 0;
  161. const uint8_t *ptr;
  162. int field_poc[2];
  163. /* set some sane default values */
  164. s->pict_type = AV_PICTURE_TYPE_I;
  165. s->key_frame = 0;
  166. s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  167. h->avctx = avctx;
  168. ff_h264_reset_sei(h);
  169. if (!buf_size)
  170. return 0;
  171. for (;;) {
  172. int src_length, dst_length, consumed;
  173. buf = avpriv_find_start_code(buf, buf_end, &state);
  174. if (buf >= buf_end)
  175. break;
  176. --buf;
  177. src_length = buf_end - buf;
  178. switch (state & 0x1f) {
  179. case NAL_SLICE:
  180. case NAL_IDR_SLICE:
  181. // Do not walk the whole buffer just to decode slice header
  182. if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
  183. /* IDR or disposable slice
  184. * No need to decode many bytes because MMCOs shall not be present. */
  185. if (src_length > 60)
  186. src_length = 60;
  187. } else {
  188. /* To decode up to MMCOs */
  189. if (src_length > 1000)
  190. src_length = 1000;
  191. }
  192. break;
  193. }
  194. ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
  195. if (ptr == NULL || dst_length < 0)
  196. break;
  197. init_get_bits(&h->gb, ptr, 8 * dst_length);
  198. switch (h->nal_unit_type) {
  199. case NAL_SPS:
  200. ff_h264_decode_seq_parameter_set(h);
  201. break;
  202. case NAL_PPS:
  203. ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
  204. break;
  205. case NAL_SEI:
  206. ff_h264_decode_sei(h);
  207. break;
  208. case NAL_IDR_SLICE:
  209. s->key_frame = 1;
  210. h->prev_frame_num = 0;
  211. h->prev_frame_num_offset = 0;
  212. h->prev_poc_msb =
  213. h->prev_poc_lsb = 0;
  214. /* fall through */
  215. case NAL_SLICE:
  216. get_ue_golomb(&h->gb); // skip first_mb_in_slice
  217. slice_type = get_ue_golomb_31(&h->gb);
  218. s->pict_type = golomb_to_pict_type[slice_type % 5];
  219. if (h->sei_recovery_frame_cnt >= 0) {
  220. /* key frame, since recovery_frame_cnt is set */
  221. s->key_frame = 1;
  222. }
  223. pps_id = get_ue_golomb(&h->gb);
  224. if (pps_id >= MAX_PPS_COUNT) {
  225. av_log(h->avctx, AV_LOG_ERROR,
  226. "pps_id %u out of range\n", pps_id);
  227. return -1;
  228. }
  229. if (!h->pps_buffers[pps_id]) {
  230. av_log(h->avctx, AV_LOG_ERROR,
  231. "non-existing PPS %u referenced\n", pps_id);
  232. return -1;
  233. }
  234. h->pps = *h->pps_buffers[pps_id];
  235. if (!h->sps_buffers[h->pps.sps_id]) {
  236. av_log(h->avctx, AV_LOG_ERROR,
  237. "non-existing SPS %u referenced\n", h->pps.sps_id);
  238. return -1;
  239. }
  240. h->sps = *h->sps_buffers[h->pps.sps_id];
  241. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  242. avctx->profile = ff_h264_get_profile(&h->sps);
  243. avctx->level = h->sps.level_idc;
  244. if (h->sps.frame_mbs_only_flag) {
  245. h->picture_structure = PICT_FRAME;
  246. } else {
  247. if (get_bits1(&h->gb)) { // field_pic_flag
  248. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  249. } else {
  250. h->picture_structure = PICT_FRAME;
  251. }
  252. }
  253. if (h->nal_unit_type == NAL_IDR_SLICE)
  254. get_ue_golomb(&h->gb); /* idr_pic_id */
  255. if (h->sps.poc_type == 0) {
  256. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  257. if (h->pps.pic_order_present == 1 &&
  258. h->picture_structure == PICT_FRAME)
  259. h->delta_poc_bottom = get_se_golomb(&h->gb);
  260. }
  261. if (h->sps.poc_type == 1 &&
  262. !h->sps.delta_pic_order_always_zero_flag) {
  263. h->delta_poc[0] = get_se_golomb(&h->gb);
  264. if (h->pps.pic_order_present == 1 &&
  265. h->picture_structure == PICT_FRAME)
  266. h->delta_poc[1] = get_se_golomb(&h->gb);
  267. }
  268. /* Decode POC of this picture.
  269. * The prev_ values needed for decoding POC of the next picture are not set here. */
  270. field_poc[0] = field_poc[1] = INT_MAX;
  271. ff_init_poc(h, field_poc, &s->output_picture_number);
  272. /* Continue parsing to check if MMCO_RESET is present.
  273. * FIXME: MMCO_RESET could appear in non-first slice.
  274. * Maybe, we should parse all undisposable non-IDR slice of this
  275. * picture until encountering MMCO_RESET in a slice of it. */
  276. if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
  277. got_reset = scan_mmco_reset(s);
  278. if (got_reset < 0)
  279. return got_reset;
  280. }
  281. /* Set up the prev_ values for decoding POC of the next picture. */
  282. h->prev_frame_num = got_reset ? 0 : h->frame_num;
  283. h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
  284. if (h->nal_ref_idc != 0) {
  285. if (!got_reset) {
  286. h->prev_poc_msb = h->poc_msb;
  287. h->prev_poc_lsb = h->poc_lsb;
  288. } else {
  289. h->prev_poc_msb = 0;
  290. h->prev_poc_lsb =
  291. h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
  292. }
  293. }
  294. if (h->sps.pic_struct_present_flag) {
  295. switch (h->sei_pic_struct) {
  296. case SEI_PIC_STRUCT_TOP_FIELD:
  297. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  298. s->repeat_pict = 0;
  299. break;
  300. case SEI_PIC_STRUCT_FRAME:
  301. case SEI_PIC_STRUCT_TOP_BOTTOM:
  302. case SEI_PIC_STRUCT_BOTTOM_TOP:
  303. s->repeat_pict = 1;
  304. break;
  305. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  306. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  307. s->repeat_pict = 2;
  308. break;
  309. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  310. s->repeat_pict = 3;
  311. break;
  312. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  313. s->repeat_pict = 5;
  314. break;
  315. default:
  316. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  317. break;
  318. }
  319. } else {
  320. s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
  321. }
  322. if (h->picture_structure == PICT_FRAME) {
  323. s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  324. if (h->sps.pic_struct_present_flag) {
  325. switch (h->sei_pic_struct) {
  326. case SEI_PIC_STRUCT_TOP_BOTTOM:
  327. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  328. s->field_order = AV_FIELD_TT;
  329. break;
  330. case SEI_PIC_STRUCT_BOTTOM_TOP:
  331. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  332. s->field_order = AV_FIELD_BB;
  333. break;
  334. default:
  335. s->field_order = AV_FIELD_PROGRESSIVE;
  336. break;
  337. }
  338. } else {
  339. if (field_poc[0] < field_poc[1])
  340. s->field_order = AV_FIELD_TT;
  341. else if (field_poc[0] > field_poc[1])
  342. s->field_order = AV_FIELD_BB;
  343. else
  344. s->field_order = AV_FIELD_PROGRESSIVE;
  345. }
  346. } else {
  347. if (h->picture_structure == PICT_TOP_FIELD)
  348. s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
  349. else
  350. s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
  351. s->field_order = AV_FIELD_UNKNOWN;
  352. }
  353. return 0; /* no need to evaluate the rest */
  354. }
  355. buf += consumed;
  356. }
  357. /* didn't find a picture! */
  358. av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
  359. return -1;
  360. }
  361. static int h264_parse(AVCodecParserContext *s,
  362. AVCodecContext *avctx,
  363. const uint8_t **poutbuf, int *poutbuf_size,
  364. const uint8_t *buf, int buf_size)
  365. {
  366. H264Context *h = s->priv_data;
  367. ParseContext *pc = &h->parse_context;
  368. int next;
  369. if (!h->got_first) {
  370. h->got_first = 1;
  371. if (avctx->extradata_size) {
  372. h->avctx = avctx;
  373. // must be done like in the decoder.
  374. // otherwise opening the parser, creating extradata,
  375. // and then closing and opening again
  376. // will cause has_b_frames to be always set.
  377. // NB: estimate_timings_from_pts behaves exactly like this.
  378. if (!avctx->has_b_frames)
  379. h->low_delay = 1;
  380. ff_h264_decode_extradata(h);
  381. }
  382. }
  383. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  384. next = buf_size;
  385. } else {
  386. next = h264_find_frame_end(h, buf, buf_size);
  387. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  388. *poutbuf = NULL;
  389. *poutbuf_size = 0;
  390. return buf_size;
  391. }
  392. if (next < 0 && next != END_NOT_FOUND) {
  393. assert(pc->last_index + next >= 0);
  394. h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
  395. }
  396. }
  397. parse_nal_units(s, avctx, buf, buf_size);
  398. if (h->sei_cpb_removal_delay >= 0) {
  399. s->dts_sync_point = h->sei_buffering_period_present;
  400. s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
  401. s->pts_dts_delta = h->sei_dpb_output_delay;
  402. } else {
  403. s->dts_sync_point = INT_MIN;
  404. s->dts_ref_dts_delta = INT_MIN;
  405. s->pts_dts_delta = INT_MIN;
  406. }
  407. if (s->flags & PARSER_FLAG_ONCE) {
  408. s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
  409. }
  410. *poutbuf = buf;
  411. *poutbuf_size = buf_size;
  412. return next;
  413. }
  414. static int h264_split(AVCodecContext *avctx,
  415. const uint8_t *buf, int buf_size)
  416. {
  417. int i;
  418. uint32_t state = -1;
  419. int has_sps = 0;
  420. for (i = 0; i <= buf_size; i++) {
  421. if ((state & 0xFFFFFF1F) == 0x107)
  422. has_sps = 1;
  423. /* if((state&0xFFFFFF1F) == 0x101 ||
  424. * (state&0xFFFFFF1F) == 0x102 ||
  425. * (state&0xFFFFFF1F) == 0x105) {
  426. * }
  427. */
  428. if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
  429. (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
  430. if (has_sps) {
  431. while (i > 4 && buf[i - 5] == 0)
  432. i--;
  433. return i - 4;
  434. }
  435. }
  436. if (i < buf_size)
  437. state = (state << 8) | buf[i];
  438. }
  439. return 0;
  440. }
  441. static void close(AVCodecParserContext *s)
  442. {
  443. H264Context *h = s->priv_data;
  444. ParseContext *pc = &h->parse_context;
  445. av_free(pc->buffer);
  446. ff_h264_free_context(h);
  447. }
  448. static av_cold int init(AVCodecParserContext *s)
  449. {
  450. H264Context *h = s->priv_data;
  451. h->thread_context[0] = h;
  452. h->slice_context_count = 1;
  453. ff_h264dsp_init(&h->h264dsp, 8, 1);
  454. return 0;
  455. }
  456. AVCodecParser ff_h264_parser = {
  457. .codec_ids = { AV_CODEC_ID_H264 },
  458. .priv_data_size = sizeof(H264Context),
  459. .parser_init = init,
  460. .parser_parse = h264_parse,
  461. .parser_close = close,
  462. .split = h264_split,
  463. };