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.

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