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.

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