cbs_h264.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVCODEC_CBS_H264_H
  19. #define AVCODEC_CBS_H264_H
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "cbs.h"
  23. #include "cbs_h2645.h"
  24. #include "h264.h"
  25. enum {
  26. // This limit is arbitrary - it is sufficient for one message of each
  27. // type plus some repeats, and will therefore easily cover all sane
  28. // streams. However, it is possible to make technically-valid streams
  29. // for which it will fail (for example, by including a large number of
  30. // user-data-unregistered messages).
  31. H264_MAX_SEI_PAYLOADS = 64,
  32. };
  33. typedef struct H264RawNALUnitHeader {
  34. uint8_t nal_ref_idc;
  35. uint8_t nal_unit_type;
  36. uint8_t svc_extension_flag;
  37. uint8_t avc_3d_extension_flag;
  38. } H264RawNALUnitHeader;
  39. typedef struct H264RawScalingList {
  40. int8_t delta_scale[64];
  41. } H264RawScalingList;
  42. typedef struct H264RawHRD {
  43. uint8_t cpb_cnt_minus1;
  44. uint8_t bit_rate_scale;
  45. uint8_t cpb_size_scale;
  46. uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT];
  47. uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT];
  48. uint8_t cbr_flag[H264_MAX_CPB_CNT];
  49. uint8_t initial_cpb_removal_delay_length_minus1;
  50. uint8_t cpb_removal_delay_length_minus1;
  51. uint8_t dpb_output_delay_length_minus1;
  52. uint8_t time_offset_length;
  53. } H264RawHRD;
  54. typedef struct H264RawVUI {
  55. uint8_t aspect_ratio_info_present_flag;
  56. uint8_t aspect_ratio_idc;
  57. uint16_t sar_width;
  58. uint16_t sar_height;
  59. uint8_t overscan_info_present_flag;
  60. uint8_t overscan_appropriate_flag;
  61. uint8_t video_signal_type_present_flag;
  62. uint8_t video_format;
  63. uint8_t video_full_range_flag;
  64. uint8_t colour_description_present_flag;
  65. uint8_t colour_primaries;
  66. uint8_t transfer_characteristics;
  67. uint8_t matrix_coefficients;
  68. uint8_t chroma_loc_info_present_flag;
  69. uint8_t chroma_sample_loc_type_top_field;
  70. uint8_t chroma_sample_loc_type_bottom_field;
  71. uint8_t timing_info_present_flag;
  72. uint32_t num_units_in_tick;
  73. uint32_t time_scale;
  74. uint8_t fixed_frame_rate_flag;
  75. uint8_t nal_hrd_parameters_present_flag;
  76. H264RawHRD nal_hrd_parameters;
  77. uint8_t vcl_hrd_parameters_present_flag;
  78. H264RawHRD vcl_hrd_parameters;
  79. uint8_t low_delay_hrd_flag;
  80. uint8_t pic_struct_present_flag;
  81. uint8_t bitstream_restriction_flag;
  82. uint8_t motion_vectors_over_pic_boundaries_flag;
  83. uint8_t max_bytes_per_pic_denom;
  84. uint8_t max_bits_per_mb_denom;
  85. uint8_t log2_max_mv_length_horizontal;
  86. uint8_t log2_max_mv_length_vertical;
  87. uint8_t max_num_reorder_frames;
  88. uint8_t max_dec_frame_buffering;
  89. } H264RawVUI;
  90. typedef struct H264RawSPS {
  91. H264RawNALUnitHeader nal_unit_header;
  92. uint8_t profile_idc;
  93. uint8_t constraint_set0_flag;
  94. uint8_t constraint_set1_flag;
  95. uint8_t constraint_set2_flag;
  96. uint8_t constraint_set3_flag;
  97. uint8_t constraint_set4_flag;
  98. uint8_t constraint_set5_flag;
  99. uint8_t reserved_zero_2bits;
  100. uint8_t level_idc;
  101. uint8_t seq_parameter_set_id;
  102. uint8_t chroma_format_idc;
  103. uint8_t separate_colour_plane_flag;
  104. uint8_t bit_depth_luma_minus8;
  105. uint8_t bit_depth_chroma_minus8;
  106. uint8_t qpprime_y_zero_transform_bypass_flag;
  107. uint8_t seq_scaling_matrix_present_flag;
  108. uint8_t seq_scaling_list_present_flag[12];
  109. H264RawScalingList scaling_list_4x4[6];
  110. H264RawScalingList scaling_list_8x8[6];
  111. uint8_t log2_max_frame_num_minus4;
  112. uint8_t pic_order_cnt_type;
  113. uint8_t log2_max_pic_order_cnt_lsb_minus4;
  114. uint8_t delta_pic_order_always_zero_flag;
  115. int32_t offset_for_non_ref_pic;
  116. int32_t offset_for_top_to_bottom_field;
  117. uint8_t num_ref_frames_in_pic_order_cnt_cycle;
  118. int32_t offset_for_ref_frame[256];
  119. uint8_t max_num_ref_frames;
  120. uint8_t gaps_in_frame_num_allowed_flag;
  121. uint16_t pic_width_in_mbs_minus1;
  122. uint16_t pic_height_in_map_units_minus1;
  123. uint8_t frame_mbs_only_flag;
  124. uint8_t mb_adaptive_frame_field_flag;
  125. uint8_t direct_8x8_inference_flag;
  126. uint8_t frame_cropping_flag;
  127. uint16_t frame_crop_left_offset;
  128. uint16_t frame_crop_right_offset;
  129. uint16_t frame_crop_top_offset;
  130. uint16_t frame_crop_bottom_offset;
  131. uint8_t vui_parameters_present_flag;
  132. H264RawVUI vui;
  133. } H264RawSPS;
  134. typedef struct H264RawSPSExtension {
  135. H264RawNALUnitHeader nal_unit_header;
  136. uint8_t seq_parameter_set_id;
  137. uint8_t aux_format_idc;
  138. uint8_t bit_depth_aux_minus8;
  139. uint8_t alpha_incr_flag;
  140. uint16_t alpha_opaque_value;
  141. uint16_t alpha_transparent_value;
  142. uint8_t additional_extension_flag;
  143. } H264RawSPSExtension;
  144. typedef struct H264RawPPS {
  145. H264RawNALUnitHeader nal_unit_header;
  146. uint8_t pic_parameter_set_id;
  147. uint8_t seq_parameter_set_id;
  148. uint8_t entropy_coding_mode_flag;
  149. uint8_t bottom_field_pic_order_in_frame_present_flag;
  150. uint8_t num_slice_groups_minus1;
  151. uint8_t slice_group_map_type;
  152. uint16_t run_length_minus1[H264_MAX_SLICE_GROUPS];
  153. uint16_t top_left[H264_MAX_SLICE_GROUPS];
  154. uint16_t bottom_right[H264_MAX_SLICE_GROUPS];
  155. uint8_t slice_group_change_direction_flag;
  156. uint16_t slice_group_change_rate_minus1;
  157. uint16_t pic_size_in_map_units_minus1;
  158. uint8_t *slice_group_id;
  159. AVBufferRef *slice_group_id_ref;
  160. uint8_t num_ref_idx_l0_default_active_minus1;
  161. uint8_t num_ref_idx_l1_default_active_minus1;
  162. uint8_t weighted_pred_flag;
  163. uint8_t weighted_bipred_idc;
  164. int8_t pic_init_qp_minus26;
  165. int8_t pic_init_qs_minus26;
  166. int8_t chroma_qp_index_offset;
  167. uint8_t deblocking_filter_control_present_flag;
  168. uint8_t constrained_intra_pred_flag;
  169. uint8_t more_rbsp_data;
  170. uint8_t redundant_pic_cnt_present_flag;
  171. uint8_t transform_8x8_mode_flag;
  172. uint8_t pic_scaling_matrix_present_flag;
  173. uint8_t pic_scaling_list_present_flag[12];
  174. H264RawScalingList scaling_list_4x4[6];
  175. H264RawScalingList scaling_list_8x8[6];
  176. int8_t second_chroma_qp_index_offset;
  177. } H264RawPPS;
  178. typedef struct H264RawAUD {
  179. H264RawNALUnitHeader nal_unit_header;
  180. uint8_t primary_pic_type;
  181. } H264RawAUD;
  182. typedef struct H264RawSEIBufferingPeriod {
  183. uint8_t seq_parameter_set_id;
  184. struct {
  185. uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT];
  186. uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT];
  187. } nal, vcl;
  188. } H264RawSEIBufferingPeriod;
  189. typedef struct H264RawSEIPicTimestamp {
  190. uint8_t ct_type;
  191. uint8_t nuit_field_based_flag;
  192. uint8_t counting_type;
  193. uint8_t full_timestamp_flag;
  194. uint8_t discontinuity_flag;
  195. uint8_t cnt_dropped_flag;
  196. uint8_t n_frames;
  197. uint8_t seconds_flag;
  198. uint8_t seconds_value;
  199. uint8_t minutes_flag;
  200. uint8_t minutes_value;
  201. uint8_t hours_flag;
  202. uint8_t hours_value;
  203. int32_t time_offset;
  204. } H264RawSEIPicTimestamp;
  205. typedef struct H264RawSEIPicTiming {
  206. uint32_t cpb_removal_delay;
  207. uint32_t dpb_output_delay;
  208. uint8_t pic_struct;
  209. uint8_t clock_timestamp_flag[3];
  210. H264RawSEIPicTimestamp timestamp[3];
  211. } H264RawSEIPicTiming;
  212. typedef struct H264RawSEIPanScanRect {
  213. uint32_t pan_scan_rect_id;
  214. uint8_t pan_scan_rect_cancel_flag;
  215. uint8_t pan_scan_cnt_minus1;
  216. int32_t pan_scan_rect_left_offset[3];
  217. int32_t pan_scan_rect_right_offset[3];
  218. int32_t pan_scan_rect_top_offset[3];
  219. int32_t pan_scan_rect_bottom_offset[3];
  220. uint16_t pan_scan_rect_repetition_period;
  221. } H264RawSEIPanScanRect;
  222. typedef struct H264RawSEIUserDataRegistered {
  223. uint8_t itu_t_t35_country_code;
  224. uint8_t itu_t_t35_country_code_extension_byte;
  225. uint8_t *data;
  226. size_t data_length;
  227. AVBufferRef *data_ref;
  228. } H264RawSEIUserDataRegistered;
  229. typedef struct H264RawSEIUserDataUnregistered {
  230. uint8_t uuid_iso_iec_11578[16];
  231. uint8_t *data;
  232. size_t data_length;
  233. AVBufferRef *data_ref;
  234. } H264RawSEIUserDataUnregistered;
  235. typedef struct H264RawSEIRecoveryPoint {
  236. uint16_t recovery_frame_cnt;
  237. uint8_t exact_match_flag;
  238. uint8_t broken_link_flag;
  239. uint8_t changing_slice_group_idc;
  240. } H264RawSEIRecoveryPoint;
  241. typedef struct H264RawSEIDisplayOrientation {
  242. uint8_t display_orientation_cancel_flag;
  243. uint8_t hor_flip;
  244. uint8_t ver_flip;
  245. uint16_t anticlockwise_rotation;
  246. uint16_t display_orientation_repetition_period;
  247. uint8_t display_orientation_extension_flag;
  248. } H264RawSEIDisplayOrientation;
  249. typedef struct H264RawSEIMasteringDisplayColourVolume {
  250. uint16_t display_primaries_x[3];
  251. uint16_t display_primaries_y[3];
  252. uint16_t white_point_x;
  253. uint16_t white_point_y;
  254. uint32_t max_display_mastering_luminance;
  255. uint32_t min_display_mastering_luminance;
  256. } H264RawSEIMasteringDisplayColourVolume;
  257. typedef struct H264RawSEIAlternativeTransferCharacteristics {
  258. uint8_t preferred_transfer_characteristics;
  259. } H264RawSEIAlternativeTransferCharacteristics;
  260. typedef struct H264RawSEIPayload {
  261. uint32_t payload_type;
  262. uint32_t payload_size;
  263. union {
  264. H264RawSEIBufferingPeriod buffering_period;
  265. H264RawSEIPicTiming pic_timing;
  266. H264RawSEIPanScanRect pan_scan_rect;
  267. // H264RawSEIFiller filler -> no fields.
  268. H264RawSEIUserDataRegistered user_data_registered;
  269. H264RawSEIUserDataUnregistered user_data_unregistered;
  270. H264RawSEIRecoveryPoint recovery_point;
  271. H264RawSEIDisplayOrientation display_orientation;
  272. H264RawSEIMasteringDisplayColourVolume mastering_display_colour_volume;
  273. H264RawSEIAlternativeTransferCharacteristics
  274. alternative_transfer_characteristics;
  275. struct {
  276. uint8_t *data;
  277. size_t data_length;
  278. AVBufferRef *data_ref;
  279. } other;
  280. } payload;
  281. } H264RawSEIPayload;
  282. typedef struct H264RawSEI {
  283. H264RawNALUnitHeader nal_unit_header;
  284. H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS];
  285. uint8_t payload_count;
  286. } H264RawSEI;
  287. typedef struct H264RawSliceHeader {
  288. H264RawNALUnitHeader nal_unit_header;
  289. uint32_t first_mb_in_slice;
  290. uint8_t slice_type;
  291. uint8_t pic_parameter_set_id;
  292. uint8_t colour_plane_id;
  293. uint16_t frame_num;
  294. uint8_t field_pic_flag;
  295. uint8_t bottom_field_flag;
  296. uint16_t idr_pic_id;
  297. uint16_t pic_order_cnt_lsb;
  298. int32_t delta_pic_order_cnt_bottom;
  299. int32_t delta_pic_order_cnt[2];
  300. uint8_t redundant_pic_cnt;
  301. uint8_t direct_spatial_mv_pred_flag;
  302. uint8_t num_ref_idx_active_override_flag;
  303. uint8_t num_ref_idx_l0_active_minus1;
  304. uint8_t num_ref_idx_l1_active_minus1;
  305. uint8_t ref_pic_list_modification_flag_l0;
  306. uint8_t ref_pic_list_modification_flag_l1;
  307. struct {
  308. uint8_t modification_of_pic_nums_idc;
  309. int32_t abs_diff_pic_num_minus1;
  310. uint8_t long_term_pic_num;
  311. } rplm_l0[H264_MAX_RPLM_COUNT], rplm_l1[H264_MAX_RPLM_COUNT];
  312. uint8_t luma_log2_weight_denom;
  313. uint8_t chroma_log2_weight_denom;
  314. uint8_t luma_weight_l0_flag[H264_MAX_REFS];
  315. int8_t luma_weight_l0[H264_MAX_REFS];
  316. int8_t luma_offset_l0[H264_MAX_REFS];
  317. uint8_t chroma_weight_l0_flag[H264_MAX_REFS];
  318. int8_t chroma_weight_l0[H264_MAX_REFS][2];
  319. int8_t chroma_offset_l0[H264_MAX_REFS][2];
  320. uint8_t luma_weight_l1_flag[H264_MAX_REFS];
  321. int8_t luma_weight_l1[H264_MAX_REFS];
  322. int8_t luma_offset_l1[H264_MAX_REFS];
  323. uint8_t chroma_weight_l1_flag[H264_MAX_REFS];
  324. int8_t chroma_weight_l1[H264_MAX_REFS][2];
  325. int8_t chroma_offset_l1[H264_MAX_REFS][2];
  326. uint8_t no_output_of_prior_pics_flag;
  327. uint8_t long_term_reference_flag;
  328. uint8_t adaptive_ref_pic_marking_mode_flag;
  329. struct {
  330. uint8_t memory_management_control_operation;
  331. int32_t difference_of_pic_nums_minus1;
  332. uint8_t long_term_pic_num;
  333. uint8_t long_term_frame_idx;
  334. uint8_t max_long_term_frame_idx_plus1;
  335. } mmco[H264_MAX_MMCO_COUNT];
  336. uint8_t cabac_init_idc;
  337. int8_t slice_qp_delta;
  338. uint8_t sp_for_switch_flag;
  339. int8_t slice_qs_delta;
  340. uint8_t disable_deblocking_filter_idc;
  341. int8_t slice_alpha_c0_offset_div2;
  342. int8_t slice_beta_offset_div2;
  343. uint16_t slice_group_change_cycle;
  344. } H264RawSliceHeader;
  345. typedef struct H264RawSlice {
  346. H264RawSliceHeader header;
  347. uint8_t *data;
  348. size_t data_size;
  349. int data_bit_start;
  350. AVBufferRef *data_ref;
  351. } H264RawSlice;
  352. typedef struct H264RawFiller {
  353. H264RawNALUnitHeader nal_unit_header;
  354. uint32_t filler_size;
  355. } H264RawFiller;
  356. typedef struct CodedBitstreamH264Context {
  357. // Reader/writer context in common with the H.265 implementation.
  358. CodedBitstreamH2645Context common;
  359. // All currently available parameter sets. These are updated when
  360. // any parameter set NAL unit is read/written with this context.
  361. AVBufferRef *sps_ref[H264_MAX_SPS_COUNT];
  362. AVBufferRef *pps_ref[H264_MAX_PPS_COUNT];
  363. H264RawSPS *sps[H264_MAX_SPS_COUNT];
  364. H264RawPPS *pps[H264_MAX_PPS_COUNT];
  365. // The currently active parameter sets. These are updated when any
  366. // NAL unit refers to the relevant parameter set. These pointers
  367. // must also be present in the arrays above.
  368. const H264RawSPS *active_sps;
  369. const H264RawPPS *active_pps;
  370. // The NAL unit type of the most recent normal slice. This is required
  371. // to be able to read/write auxiliary slices, because IdrPicFlag is
  372. // otherwise unknown.
  373. uint8_t last_slice_nal_unit_type;
  374. } CodedBitstreamH264Context;
  375. /**
  376. * Add an SEI message to an access unit.
  377. *
  378. * On success, the payload will be owned by a unit in access_unit;
  379. * on failure, the content of the payload will be freed.
  380. */
  381. int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *access_unit,
  382. H264RawSEIPayload *payload);
  383. /**
  384. * Delete an SEI message from an access unit.
  385. *
  386. * Deletes from nal_unit, which must be an SEI NAL unit. If this is the
  387. * last message in nal_unit, also deletes it from access_unit.
  388. *
  389. * Requires nal_unit to be a unit in access_unit and position to be >= 0
  390. * and < the payload count of the SEI nal_unit.
  391. */
  392. void ff_cbs_h264_delete_sei_message(CodedBitstreamFragment *access_unit,
  393. CodedBitstreamUnit *nal_unit,
  394. int position);
  395. #endif /* AVCODEC_CBS_H264_H */