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.

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