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.

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