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.

394 lines
13KB

  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 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 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. h->sei_reguserdata_afd_present = 0;
  42. h->a53_caption_size = 0;
  43. av_freep(&h->a53_caption);
  44. }
  45. static int decode_picture_timing(H264Context *h)
  46. {
  47. if (h->sps.nal_hrd_parameters_present_flag ||
  48. h->sps.vcl_hrd_parameters_present_flag) {
  49. h->sei_cpb_removal_delay = get_bits(&h->gb,
  50. h->sps.cpb_removal_delay_length);
  51. h->sei_dpb_output_delay = get_bits(&h->gb,
  52. h->sps.dpb_output_delay_length);
  53. }
  54. if (h->sps.pic_struct_present_flag) {
  55. unsigned int i, num_clock_ts;
  56. h->sei_pic_struct = get_bits(&h->gb, 4);
  57. h->sei_ct_type = 0;
  58. if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
  59. return AVERROR_INVALIDDATA;
  60. num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
  61. for (i = 0; i < num_clock_ts; i++) {
  62. if (get_bits(&h->gb, 1)) { /* clock_timestamp_flag */
  63. unsigned int full_timestamp_flag;
  64. h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
  65. skip_bits(&h->gb, 1); /* nuit_field_based_flag */
  66. skip_bits(&h->gb, 5); /* counting_type */
  67. full_timestamp_flag = get_bits(&h->gb, 1);
  68. skip_bits(&h->gb, 1); /* discontinuity_flag */
  69. skip_bits(&h->gb, 1); /* cnt_dropped_flag */
  70. skip_bits(&h->gb, 8); /* n_frames */
  71. if (full_timestamp_flag) {
  72. skip_bits(&h->gb, 6); /* seconds_value 0..59 */
  73. skip_bits(&h->gb, 6); /* minutes_value 0..59 */
  74. skip_bits(&h->gb, 5); /* hours_value 0..23 */
  75. } else {
  76. if (get_bits(&h->gb, 1)) { /* seconds_flag */
  77. skip_bits(&h->gb, 6); /* seconds_value range 0..59 */
  78. if (get_bits(&h->gb, 1)) { /* minutes_flag */
  79. skip_bits(&h->gb, 6); /* minutes_value 0..59 */
  80. if (get_bits(&h->gb, 1)) /* hours_flag */
  81. skip_bits(&h->gb, 5); /* hours_value 0..23 */
  82. }
  83. }
  84. }
  85. if (h->sps.time_offset_length > 0)
  86. skip_bits(&h->gb,
  87. h->sps.time_offset_length); /* time_offset */
  88. }
  89. }
  90. if (h->avctx->debug & FF_DEBUG_PICT_INFO)
  91. av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
  92. h->sei_ct_type, h->sei_pic_struct);
  93. }
  94. return 0;
  95. }
  96. static int decode_registered_user_data_afd(H264Context *h, int size)
  97. {
  98. int flag;
  99. if (size-- < 1)
  100. return AVERROR_INVALIDDATA;
  101. skip_bits(&h->gb, 1); // 0
  102. flag = get_bits(&h->gb, 1); // active_format_flag
  103. skip_bits(&h->gb, 6); // reserved
  104. if (flag) {
  105. if (size-- < 1)
  106. return AVERROR_INVALIDDATA;
  107. skip_bits(&h->gb, 4); // reserved
  108. h->active_format_description = get_bits(&h->gb, 4);
  109. h->sei_reguserdata_afd_present = 1;
  110. }
  111. return 0;
  112. }
  113. static int decode_registered_user_data_closed_caption(H264Context *h, int size)
  114. {
  115. int flag;
  116. int user_data_type_code;
  117. int cc_count;
  118. if (size < 3)
  119. return AVERROR(EINVAL);
  120. user_data_type_code = get_bits(&h->gb, 8);
  121. if (user_data_type_code == 0x3) {
  122. skip_bits(&h->gb, 1); // reserved
  123. flag = get_bits(&h->gb, 1); // process_cc_data_flag
  124. if (flag) {
  125. skip_bits(&h->gb, 1); // zero bit
  126. cc_count = get_bits(&h->gb, 5);
  127. skip_bits(&h->gb, 8); // reserved
  128. size -= 2;
  129. if (cc_count && size >= cc_count * 3) {
  130. const uint64_t new_size = (h->a53_caption_size + cc_count
  131. * UINT64_C(3));
  132. int i, ret;
  133. if (new_size > INT_MAX)
  134. return AVERROR(EINVAL);
  135. /* Allow merging of the cc data from two fields. */
  136. ret = av_reallocp(&h->a53_caption, new_size);
  137. if (ret < 0)
  138. return ret;
  139. for (i = 0; i < cc_count; i++) {
  140. h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
  141. h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
  142. h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
  143. }
  144. skip_bits(&h->gb, 8); // marker_bits
  145. }
  146. }
  147. } else {
  148. int i;
  149. avpriv_request_sample(h->avctx, "Subtitles with data type 0x%02x",
  150. user_data_type_code);
  151. for (i = 0; i < size - 1; i++)
  152. skip_bits(&h->gb, 8);
  153. }
  154. return 0;
  155. }
  156. static int decode_registered_user_data(H264Context *h, int size)
  157. {
  158. uint32_t country_code;
  159. uint32_t user_identifier;
  160. if (size < 7)
  161. return AVERROR_INVALIDDATA;
  162. size -= 7;
  163. country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
  164. if (country_code == 0xFF) {
  165. skip_bits(&h->gb, 8); // itu_t_t35_country_code_extension_byte
  166. size--;
  167. }
  168. /* itu_t_t35_payload_byte follows */
  169. skip_bits(&h->gb, 8); // terminal provider code
  170. skip_bits(&h->gb, 8); // terminal provider oriented code
  171. user_identifier = get_bits_long(&h->gb, 32);
  172. switch (user_identifier) {
  173. case MKBETAG('D', 'T', 'G', '1'): // afd_data
  174. return decode_registered_user_data_afd(h, size);
  175. case MKBETAG('G', 'A', '9', '4'): // closed captions
  176. return decode_registered_user_data_closed_caption(h, size);
  177. default:
  178. skip_bits(&h->gb, size * 8);
  179. break;
  180. }
  181. return 0;
  182. }
  183. static int decode_unregistered_user_data(H264Context *h, int size)
  184. {
  185. uint8_t *user_data;
  186. int e, build, i;
  187. if (size < 16 || size >= INT_MAX - 16)
  188. return AVERROR_INVALIDDATA;
  189. user_data = av_malloc(16 + size + 1);
  190. if (!user_data)
  191. return AVERROR(ENOMEM);
  192. for (i = 0; i < size + 16; i++)
  193. user_data[i] = get_bits(&h->gb, 8);
  194. user_data[i] = 0;
  195. e = sscanf(user_data + 16, "x264 - core %d", &build);
  196. if (e == 1 && build > 0)
  197. h->x264_build = build;
  198. if (strlen(user_data + 16) > 0)
  199. av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
  200. av_free(user_data);
  201. return 0;
  202. }
  203. static int decode_recovery_point(H264Context *h)
  204. {
  205. h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
  206. /* 1b exact_match_flag,
  207. * 1b broken_link_flag,
  208. * 2b changing_slice_group_idc */
  209. skip_bits(&h->gb, 4);
  210. return 0;
  211. }
  212. static int decode_buffering_period(H264Context *h)
  213. {
  214. unsigned int sps_id;
  215. int sched_sel_idx;
  216. SPS *sps;
  217. sps_id = get_ue_golomb_31(&h->gb);
  218. if (sps_id > 31 || !h->sps_buffers[sps_id]) {
  219. av_log(h->avctx, AV_LOG_ERROR,
  220. "non-existing SPS %d referenced in buffering period\n", sps_id);
  221. return AVERROR_INVALIDDATA;
  222. }
  223. sps = h->sps_buffers[sps_id];
  224. // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
  225. if (sps->nal_hrd_parameters_present_flag) {
  226. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  227. h->initial_cpb_removal_delay[sched_sel_idx] =
  228. get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  229. // initial_cpb_removal_delay_offset
  230. skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  231. }
  232. }
  233. if (sps->vcl_hrd_parameters_present_flag) {
  234. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  235. h->initial_cpb_removal_delay[sched_sel_idx] =
  236. get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  237. // initial_cpb_removal_delay_offset
  238. skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
  239. }
  240. }
  241. h->sei_buffering_period_present = 1;
  242. return 0;
  243. }
  244. static int decode_frame_packing_arrangement(H264Context *h)
  245. {
  246. get_ue_golomb(&h->gb); // frame_packing_arrangement_id
  247. h->sei_frame_packing_present = !get_bits1(&h->gb);
  248. if (h->sei_frame_packing_present) {
  249. h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
  250. h->quincunx_subsampling = get_bits1(&h->gb);
  251. h->content_interpretation_type = get_bits(&h->gb, 6);
  252. // the following skips: spatial_flipping_flag, frame0_flipped_flag,
  253. // field_views_flag, current_frame_is_frame0_flag,
  254. // frame0_self_contained_flag, frame1_self_contained_flag
  255. skip_bits(&h->gb, 6);
  256. if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
  257. skip_bits(&h->gb, 16); // frame[01]_grid_position_[xy]
  258. skip_bits(&h->gb, 8); // frame_packing_arrangement_reserved_byte
  259. get_ue_golomb(&h->gb); // frame_packing_arrangement_repetition_period
  260. }
  261. skip_bits1(&h->gb); // frame_packing_arrangement_extension_flag
  262. return 0;
  263. }
  264. static int decode_display_orientation(H264Context *h)
  265. {
  266. h->sei_display_orientation_present = !get_bits1(&h->gb);
  267. if (h->sei_display_orientation_present) {
  268. h->sei_hflip = get_bits1(&h->gb); // hor_flip
  269. h->sei_vflip = get_bits1(&h->gb); // ver_flip
  270. h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
  271. get_ue_golomb(&h->gb); // display_orientation_repetition_period
  272. skip_bits1(&h->gb); // display_orientation_extension_flag
  273. }
  274. return 0;
  275. }
  276. int ff_h264_decode_sei(H264Context *h)
  277. {
  278. while (get_bits_left(&h->gb) > 16) {
  279. int size = 0;
  280. int type = 0;
  281. int ret = 0;
  282. int last = 0;
  283. while (get_bits_left(&h->gb) >= 8 &&
  284. (last = get_bits(&h->gb, 8)) == 255) {
  285. type += 255;
  286. }
  287. type += last;
  288. last = 0;
  289. while (get_bits_left(&h->gb) >= 8 &&
  290. (last = get_bits(&h->gb, 8)) == 255) {
  291. size += 255;
  292. }
  293. size += last;
  294. if (size > get_bits_left(&h->gb) / 8) {
  295. av_log(h->avctx, AV_LOG_ERROR, "SEI type %d truncated at %d\n",
  296. type, get_bits_left(&h->gb));
  297. return AVERROR_INVALIDDATA;
  298. }
  299. switch (type) {
  300. case SEI_TYPE_PIC_TIMING: // Picture timing SEI
  301. ret = decode_picture_timing(h);
  302. break;
  303. case SEI_TYPE_USER_DATA_REGISTERED:
  304. ret = decode_registered_user_data(h, size);
  305. break;
  306. case SEI_TYPE_USER_DATA_UNREGISTERED:
  307. ret = decode_unregistered_user_data(h, size);
  308. break;
  309. case SEI_TYPE_RECOVERY_POINT:
  310. ret = decode_recovery_point(h);
  311. break;
  312. case SEI_TYPE_BUFFERING_PERIOD:
  313. ret = decode_buffering_period(h);
  314. break;
  315. case SEI_TYPE_FRAME_PACKING:
  316. ret = decode_frame_packing_arrangement(h);
  317. break;
  318. case SEI_TYPE_DISPLAY_ORIENTATION:
  319. ret = decode_display_orientation(h);
  320. break;
  321. default:
  322. av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
  323. skip_bits(&h->gb, 8 * size);
  324. }
  325. if (ret < 0)
  326. return ret;
  327. // FIXME check bits here
  328. align_get_bits(&h->gb);
  329. }
  330. return 0;
  331. }