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.

403 lines
14KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 sei decoding.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avcodec.h"
  27. #include "golomb.h"
  28. #include "h264.h"
  29. #include "internal.h"
  30. static const uint8_t sei_num_clock_ts_table[9] = {
  31. 1, 1, 1, 2, 2, 3, 3, 2, 3
  32. };
  33. void ff_h264_reset_sei(H264Context *h)
  34. {
  35. h->sei_recovery_frame_cnt = -1;
  36. h->sei_dpb_output_delay = 0;
  37. h->sei_cpb_removal_delay = -1;
  38. h->sei_buffering_period_present = 0;
  39. h->sei_frame_packing_present = 0;
  40. h->sei_display_orientation_present = 0;
  41. }
  42. static int decode_picture_timing(H264Context *h)
  43. {
  44. SPS *sps = &h->sps;
  45. int i;
  46. for (i = 0; i<MAX_SPS_COUNT; i++)
  47. if (!sps->log2_max_frame_num && h->sps_buffers[i])
  48. sps = h->sps_buffers[i];
  49. if (sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag) {
  50. h->sei_cpb_removal_delay = get_bits_long(&h->gb,
  51. sps->cpb_removal_delay_length);
  52. h->sei_dpb_output_delay = get_bits_long(&h->gb,
  53. sps->dpb_output_delay_length);
  54. }
  55. if (sps->pic_struct_present_flag) {
  56. unsigned int i, num_clock_ts;
  57. h->sei_pic_struct = get_bits(&h->gb, 4);
  58. h->sei_ct_type = 0;
  59. if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
  60. return AVERROR_INVALIDDATA;
  61. num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
  62. for (i = 0; i < num_clock_ts; i++) {
  63. if (get_bits(&h->gb, 1)) { /* clock_timestamp_flag */
  64. unsigned int full_timestamp_flag;
  65. h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
  66. skip_bits(&h->gb, 1); /* nuit_field_based_flag */
  67. skip_bits(&h->gb, 5); /* counting_type */
  68. full_timestamp_flag = get_bits(&h->gb, 1);
  69. skip_bits(&h->gb, 1); /* discontinuity_flag */
  70. skip_bits(&h->gb, 1); /* cnt_dropped_flag */
  71. skip_bits(&h->gb, 8); /* n_frames */
  72. if (full_timestamp_flag) {
  73. skip_bits(&h->gb, 6); /* seconds_value 0..59 */
  74. skip_bits(&h->gb, 6); /* minutes_value 0..59 */
  75. skip_bits(&h->gb, 5); /* hours_value 0..23 */
  76. } else {
  77. if (get_bits(&h->gb, 1)) { /* seconds_flag */
  78. skip_bits(&h->gb, 6); /* seconds_value range 0..59 */
  79. if (get_bits(&h->gb, 1)) { /* minutes_flag */
  80. skip_bits(&h->gb, 6); /* minutes_value 0..59 */
  81. if (get_bits(&h->gb, 1)) /* hours_flag */
  82. skip_bits(&h->gb, 5); /* hours_value 0..23 */
  83. }
  84. }
  85. }
  86. if (sps->time_offset_length > 0)
  87. skip_bits(&h->gb,
  88. sps->time_offset_length); /* time_offset */
  89. }
  90. }
  91. if (h->avctx->debug & FF_DEBUG_PICT_INFO)
  92. av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
  93. h->sei_ct_type, h->sei_pic_struct);
  94. }
  95. return 0;
  96. }
  97. static int decode_user_data_itu_t_t35(H264Context *h, int size)
  98. {
  99. uint32_t user_identifier;
  100. int dtg_active_format;
  101. if (size < 7)
  102. return -1;
  103. size -= 7;
  104. skip_bits(&h->gb, 8); // country_code
  105. skip_bits(&h->gb, 16); // provider_code
  106. user_identifier = get_bits_long(&h->gb, 32);
  107. switch (user_identifier) {
  108. case 0x44544731: // "DTG1" - AFD_data
  109. if (size < 1)
  110. return -1;
  111. skip_bits(&h->gb, 1);
  112. if (get_bits(&h->gb, 1)) {
  113. skip_bits(&h->gb, 6);
  114. if (size < 2)
  115. return -1;
  116. skip_bits(&h->gb, 4);
  117. dtg_active_format = get_bits(&h->gb, 4);
  118. h->avctx->dtg_active_format = dtg_active_format;
  119. } else {
  120. skip_bits(&h->gb, 6);
  121. }
  122. break;
  123. default:
  124. skip_bits(&h->gb, size * 8);
  125. break;
  126. }
  127. return 0;
  128. }
  129. static int decode_unregistered_user_data(H264Context *h, int size)
  130. {
  131. uint8_t user_data[16 + 256];
  132. int e, build, i;
  133. if (size < 16)
  134. return AVERROR_INVALIDDATA;
  135. for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
  136. user_data[i] = get_bits(&h->gb, 8);
  137. user_data[i] = 0;
  138. e = sscanf(user_data + 16, "x264 - core %d", &build);
  139. if (e == 1 && build > 0)
  140. h->x264_build = build;
  141. if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
  142. h->x264_build = 67;
  143. if (h->avctx->debug & FF_DEBUG_BUGS)
  144. av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
  145. for (; i < size; i++)
  146. skip_bits(&h->gb, 8);
  147. return 0;
  148. }
  149. static int decode_recovery_point(H264Context *h)
  150. {
  151. h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
  152. /* 1b exact_match_flag,
  153. * 1b broken_link_flag,
  154. * 2b changing_slice_group_idc */
  155. skip_bits(&h->gb, 4);
  156. if (h->avctx->debug & FF_DEBUG_PICT_INFO)
  157. av_log(h->avctx, AV_LOG_DEBUG, "sei_recovery_frame_cnt: %d\n", h->sei_recovery_frame_cnt);
  158. h->has_recovery_point = 1;
  159. return 0;
  160. }
  161. static int decode_buffering_period(H264Context *h)
  162. {
  163. unsigned int sps_id;
  164. int sched_sel_idx;
  165. SPS *sps;
  166. sps_id = get_ue_golomb_31(&h->gb);
  167. if (sps_id > 31 || !h->sps_buffers[sps_id]) {
  168. av_log(h->avctx, AV_LOG_ERROR,
  169. "non-existing SPS %d referenced in buffering period\n", sps_id);
  170. return AVERROR_INVALIDDATA;
  171. }
  172. sps = h->sps_buffers[sps_id];
  173. // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
  174. if (sps->nal_hrd_parameters_present_flag) {
  175. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  176. h->initial_cpb_removal_delay[sched_sel_idx] =
  177. get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
  178. // initial_cpb_removal_delay_offset
  179. skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  180. }
  181. }
  182. if (sps->vcl_hrd_parameters_present_flag) {
  183. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  184. h->initial_cpb_removal_delay[sched_sel_idx] =
  185. get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
  186. // initial_cpb_removal_delay_offset
  187. skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  188. }
  189. }
  190. h->sei_buffering_period_present = 1;
  191. return 0;
  192. }
  193. static int decode_frame_packing_arrangement(H264Context *h)
  194. {
  195. h->sei_fpa.frame_packing_arrangement_id = get_ue_golomb(&h->gb);
  196. h->sei_fpa.frame_packing_arrangement_cancel_flag = get_bits1(&h->gb);
  197. h->sei_frame_packing_present = !h->sei_fpa.frame_packing_arrangement_cancel_flag;
  198. if (h->sei_frame_packing_present) {
  199. h->sei_fpa.frame_packing_arrangement_type =
  200. h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
  201. h->sei_fpa.quincunx_sampling_flag =
  202. h->quincunx_subsampling = get_bits1(&h->gb);
  203. h->sei_fpa.content_interpretation_type =
  204. h->content_interpretation_type = get_bits(&h->gb, 6);
  205. // the following skips: spatial_flipping_flag, frame0_flipped_flag,
  206. // field_views_flag, current_frame_is_frame0_flag,
  207. // frame0_self_contained_flag, frame1_self_contained_flag
  208. skip_bits(&h->gb, 6);
  209. if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
  210. skip_bits(&h->gb, 16); // frame[01]_grid_position_[xy]
  211. skip_bits(&h->gb, 8); // frame_packing_arrangement_reserved_byte
  212. h->sei_fpa.frame_packing_arrangement_repetition_period = get_ue_golomb(&h->gb) /* frame_packing_arrangement_repetition_period */;
  213. }
  214. skip_bits1(&h->gb); // frame_packing_arrangement_extension_flag
  215. if (h->avctx->debug & FF_DEBUG_PICT_INFO)
  216. av_log(h->avctx, AV_LOG_DEBUG, "SEI FPA %d %d %d %d %d %d\n",
  217. h->sei_fpa.frame_packing_arrangement_id,
  218. h->sei_fpa.frame_packing_arrangement_cancel_flag,
  219. h->sei_fpa.frame_packing_arrangement_type,
  220. h->sei_fpa.quincunx_sampling_flag,
  221. h->sei_fpa.content_interpretation_type,
  222. h->sei_fpa.frame_packing_arrangement_repetition_period);
  223. return 0;
  224. }
  225. static int decode_display_orientation(H264Context *h)
  226. {
  227. h->sei_display_orientation_present = !get_bits1(&h->gb);
  228. if (h->sei_display_orientation_present) {
  229. h->sei_hflip = get_bits1(&h->gb); // hor_flip
  230. h->sei_vflip = get_bits1(&h->gb); // ver_flip
  231. h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
  232. get_ue_golomb(&h->gb); // display_orientation_repetition_period
  233. skip_bits1(&h->gb); // display_orientation_extension_flag
  234. }
  235. return 0;
  236. }
  237. int ff_h264_decode_sei(H264Context *h)
  238. {
  239. while (get_bits_left(&h->gb) > 16) {
  240. int type = 0;
  241. unsigned size = 0;
  242. unsigned next;
  243. int ret = 0;
  244. do {
  245. if (get_bits_left(&h->gb) < 8)
  246. return AVERROR_INVALIDDATA;
  247. type += show_bits(&h->gb, 8);
  248. } while (get_bits(&h->gb, 8) == 255);
  249. do {
  250. if (get_bits_left(&h->gb) < 8)
  251. return AVERROR_INVALIDDATA;
  252. size += show_bits(&h->gb, 8);
  253. } while (get_bits(&h->gb, 8) == 255);
  254. if (h->avctx->debug&FF_DEBUG_STARTCODE)
  255. av_log(h->avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
  256. if (size > get_bits_left(&h->gb) / 8) {
  257. av_log(h->avctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
  258. type, 8*size, get_bits_left(&h->gb));
  259. return AVERROR_INVALIDDATA;
  260. }
  261. next = get_bits_count(&h->gb) + 8 * size;
  262. switch (type) {
  263. case SEI_TYPE_PIC_TIMING: // Picture timing SEI
  264. ret = decode_picture_timing(h);
  265. if (ret < 0)
  266. return ret;
  267. break;
  268. case SEI_TYPE_USER_DATA_ITU_T_T35:
  269. if (decode_user_data_itu_t_t35(h, size) < 0)
  270. return -1;
  271. break;
  272. case SEI_TYPE_USER_DATA_UNREGISTERED:
  273. ret = decode_unregistered_user_data(h, size);
  274. if (ret < 0)
  275. return ret;
  276. break;
  277. case SEI_TYPE_RECOVERY_POINT:
  278. ret = decode_recovery_point(h);
  279. if (ret < 0)
  280. return ret;
  281. break;
  282. case SEI_TYPE_BUFFERING_PERIOD:
  283. ret = decode_buffering_period(h);
  284. if (ret < 0)
  285. return ret;
  286. break;
  287. case SEI_TYPE_FRAME_PACKING:
  288. ret = decode_frame_packing_arrangement(h);
  289. if (ret < 0)
  290. return ret;
  291. break;
  292. case SEI_TYPE_DISPLAY_ORIENTATION:
  293. ret = decode_display_orientation(h);
  294. if (ret < 0)
  295. return ret;
  296. break;
  297. default:
  298. av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
  299. }
  300. skip_bits_long(&h->gb, next - get_bits_count(&h->gb));
  301. // FIXME check bits here
  302. align_get_bits(&h->gb);
  303. }
  304. return 0;
  305. }
  306. const char* ff_h264_sei_stereo_mode(H264Context *h)
  307. {
  308. if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 0) {
  309. switch (h->sei_fpa.frame_packing_arrangement_type) {
  310. case SEI_FPA_TYPE_CHECKERBOARD:
  311. if (h->sei_fpa.content_interpretation_type == 2)
  312. return "checkerboard_rl";
  313. else
  314. return "checkerboard_lr";
  315. case SEI_FPA_TYPE_INTERLEAVE_COLUMN:
  316. if (h->sei_fpa.content_interpretation_type == 2)
  317. return "col_interleaved_rl";
  318. else
  319. return "col_interleaved_lr";
  320. case SEI_FPA_TYPE_INTERLEAVE_ROW:
  321. if (h->sei_fpa.content_interpretation_type == 2)
  322. return "row_interleaved_rl";
  323. else
  324. return "row_interleaved_lr";
  325. case SEI_FPA_TYPE_SIDE_BY_SIDE:
  326. if (h->sei_fpa.content_interpretation_type == 2)
  327. return "right_left";
  328. else
  329. return "left_right";
  330. case SEI_FPA_TYPE_TOP_BOTTOM:
  331. if (h->sei_fpa.content_interpretation_type == 2)
  332. return "bottom_top";
  333. else
  334. return "top_bottom";
  335. case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
  336. if (h->sei_fpa.content_interpretation_type == 2)
  337. return "block_rl";
  338. else
  339. return "block_lr";
  340. case SEI_FPA_TYPE_2D:
  341. default:
  342. return "mono";
  343. }
  344. } else if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 1) {
  345. return "mono";
  346. } else {
  347. return NULL;
  348. }
  349. }