Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 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 | |
| 19 | #include "golomb.h" |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 20 | #include "evc.h" |
| 21 | #include "evc_parse.h" |
| 22 | |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 23 | // @see ISO_IEC_23094-1 (7.3.2.6 Slice layer RBSP syntax) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 24 | int ff_evc_parse_slice_header(GetBitContext *gb, EVCParserSliceHeader *sh, |
| 25 | const EVCParamSets *ps, enum EVCNALUnitType nalu_type) |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 26 | { |
James Almer | a5663f2 | 2023-06-17 21:02:26 | [diff] [blame] | 27 | const EVCParserPPS *pps; |
| 28 | const EVCParserSPS *sps; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 29 | int num_tiles_in_slice = 0; |
James Almer | 5b966be | 2023-06-22 22:46:22 | [diff] [blame] | 30 | unsigned slice_pic_parameter_set_id; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 31 | |
James Almer | 5b966be | 2023-06-22 22:46:22 | [diff] [blame] | 32 | slice_pic_parameter_set_id = get_ue_golomb_31(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 33 | |
James Almer | 5b966be | 2023-06-22 22:46:22 | [diff] [blame] | 34 | if (slice_pic_parameter_set_id >= EVC_MAX_PPS_COUNT) |
James Almer | 44f2631 | 2023-06-15 14:03:51 | [diff] [blame] | 35 | return AVERROR_INVALIDDATA; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 36 | |
James Almer | a5663f2 | 2023-06-17 21:02:26 | [diff] [blame] | 37 | pps = ps->pps[slice_pic_parameter_set_id]; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 38 | if(!pps) |
James Almer | 44f2631 | 2023-06-15 14:03:51 | [diff] [blame] | 39 | return AVERROR_INVALIDDATA; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 40 | |
James Almer | a5663f2 | 2023-06-17 21:02:26 | [diff] [blame] | 41 | sps = ps->sps[pps->pps_seq_parameter_set_id]; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 42 | if(!sps) |
James Almer | 44f2631 | 2023-06-15 14:03:51 | [diff] [blame] | 43 | return AVERROR_INVALIDDATA; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 44 | |
James Almer | 44f2631 | 2023-06-15 14:03:51 | [diff] [blame] | 45 | memset(sh, 0, sizeof(*sh)); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 46 | sh->slice_pic_parameter_set_id = slice_pic_parameter_set_id; |
| 47 | |
| 48 | if (!pps->single_tile_in_pic_flag) { |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 49 | sh->single_tile_in_slice_flag = get_bits1(gb); |
| 50 | sh->first_tile_id = get_bits(gb, pps->tile_id_len_minus1 + 1); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 51 | } else |
| 52 | sh->single_tile_in_slice_flag = 1; |
| 53 | |
| 54 | if (!sh->single_tile_in_slice_flag) { |
| 55 | if (pps->arbitrary_slice_present_flag) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 56 | sh->arbitrary_slice_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 57 | |
| 58 | if (!sh->arbitrary_slice_flag) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 59 | sh->last_tile_id = get_bits(gb, pps->tile_id_len_minus1 + 1); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 60 | else { |
Michael Niedermayer | ac4e3e1 | 2023-10-04 22:59:19 | [diff] [blame] | 61 | unsigned num_remaining_tiles_in_slice_minus1 = get_ue_golomb_long(gb); |
| 62 | if (num_remaining_tiles_in_slice_minus1 > EVC_MAX_TILE_ROWS * EVC_MAX_TILE_COLUMNS - 2) |
| 63 | return AVERROR_INVALIDDATA; |
| 64 | |
| 65 | num_tiles_in_slice = num_remaining_tiles_in_slice_minus1 + 2; |
| 66 | sh->num_remaining_tiles_in_slice_minus1 = num_remaining_tiles_in_slice_minus1; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 67 | for (int i = 0; i < num_tiles_in_slice - 1; ++i) |
James Almer | 5b966be | 2023-06-22 22:46:22 | [diff] [blame] | 68 | sh->delta_tile_id_minus1[i] = get_ue_golomb_long(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
James Almer | 5b966be | 2023-06-22 22:46:22 | [diff] [blame] | 72 | sh->slice_type = get_ue_golomb_31(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 73 | |
James Almer | a5663f2 | 2023-06-17 21:02:26 | [diff] [blame] | 74 | if (nalu_type == EVC_IDR_NUT) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 75 | sh->no_output_of_prior_pics_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 76 | |
| 77 | if (sps->sps_mmvd_flag && ((sh->slice_type == EVC_SLICE_TYPE_B) || (sh->slice_type == EVC_SLICE_TYPE_P))) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 78 | sh->mmvd_group_enable_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 79 | else |
| 80 | sh->mmvd_group_enable_flag = 0; |
| 81 | |
| 82 | if (sps->sps_alf_flag) { |
| 83 | int ChromaArrayType = sps->chroma_format_idc; |
| 84 | |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 85 | sh->slice_alf_enabled_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 86 | |
| 87 | if (sh->slice_alf_enabled_flag) { |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 88 | sh->slice_alf_luma_aps_id = get_bits(gb, 5); |
| 89 | sh->slice_alf_map_flag = get_bits1(gb); |
| 90 | sh->slice_alf_chroma_idc = get_bits(gb, 2); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 91 | |
| 92 | if ((ChromaArrayType == 1 || ChromaArrayType == 2) && sh->slice_alf_chroma_idc > 0) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 93 | sh->slice_alf_chroma_aps_id = get_bits(gb, 5); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 94 | } |
| 95 | if (ChromaArrayType == 3) { |
| 96 | int sliceChromaAlfEnabledFlag = 0; |
| 97 | int sliceChroma2AlfEnabledFlag = 0; |
| 98 | |
| 99 | if (sh->slice_alf_chroma_idc == 1) { // @see ISO_IEC_23094-1 (7.4.5) |
| 100 | sliceChromaAlfEnabledFlag = 1; |
| 101 | sliceChroma2AlfEnabledFlag = 0; |
| 102 | } else if (sh->slice_alf_chroma_idc == 2) { |
| 103 | sliceChromaAlfEnabledFlag = 0; |
| 104 | sliceChroma2AlfEnabledFlag = 1; |
| 105 | } else if (sh->slice_alf_chroma_idc == 3) { |
| 106 | sliceChromaAlfEnabledFlag = 1; |
| 107 | sliceChroma2AlfEnabledFlag = 1; |
| 108 | } else { |
| 109 | sliceChromaAlfEnabledFlag = 0; |
| 110 | sliceChroma2AlfEnabledFlag = 0; |
| 111 | } |
| 112 | |
| 113 | if (!sh->slice_alf_enabled_flag) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 114 | sh->slice_alf_chroma_idc = get_bits(gb, 2); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 115 | |
| 116 | if (sliceChromaAlfEnabledFlag) { |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 117 | sh->slice_alf_chroma_aps_id = get_bits(gb, 5); |
| 118 | sh->slice_alf_chroma_map_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (sliceChroma2AlfEnabledFlag) { |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 122 | sh->slice_alf_chroma2_aps_id = get_bits(gb, 5); |
| 123 | sh->slice_alf_chroma2_map_flag = get_bits1(gb); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
James Almer | a5663f2 | 2023-06-17 21:02:26 | [diff] [blame] | 128 | if (nalu_type != EVC_IDR_NUT) { |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 129 | if (sps->sps_pocs_flag) |
James Almer | e5da2ec | 2023-06-20 14:05:38 | [diff] [blame] | 130 | sh->slice_pic_order_cnt_lsb = get_bits(gb, sps->log2_max_pic_order_cnt_lsb_minus4 + 4); |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // @note |
| 134 | // If necessary, add the missing fields to the EVCParserSliceHeader structure |
| 135 | // and then extend parser implementation |
| 136 | |
James Almer | 44f2631 | 2023-06-15 14:03:51 | [diff] [blame] | 137 | return 0; |
Dawid Kozinski | 34e4f18 | 2023-06-15 11:46:43 | [diff] [blame] | 138 | } |
| 139 | |
James Almer | ff7a4cd | 2023-06-17 20:18:04 | [diff] [blame] | 140 | int ff_evc_derive_poc(const EVCParamSets *ps, const EVCParserSliceHeader *sh, |
| 141 | EVCParserPoc *poc, enum EVCNALUnitType nalu_type, int tid) |
| 142 | { |
| 143 | const EVCParserPPS *pps = ps->pps[sh->slice_pic_parameter_set_id]; |
| 144 | const EVCParserSPS *sps; |
| 145 | |
| 146 | if (!pps) |
| 147 | return AVERROR_INVALIDDATA; |
| 148 | |
| 149 | sps = ps->sps[pps->pps_seq_parameter_set_id]; |
| 150 | if (!sps) |
| 151 | return AVERROR_INVALIDDATA; |
| 152 | |
| 153 | if (sps->sps_pocs_flag) { |
| 154 | int PicOrderCntMsb = 0; |
| 155 | poc->prevPicOrderCntVal = poc->PicOrderCntVal; |
| 156 | |
| 157 | if (nalu_type == EVC_IDR_NUT) |
| 158 | PicOrderCntMsb = 0; |
| 159 | else { |
| 160 | int MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); |
| 161 | int prevPicOrderCntLsb = poc->PicOrderCntVal & (MaxPicOrderCntLsb - 1); |
| 162 | int prevPicOrderCntMsb = poc->PicOrderCntVal - prevPicOrderCntLsb; |
| 163 | |
| 164 | if ((sh->slice_pic_order_cnt_lsb < prevPicOrderCntLsb) && |
| 165 | ((prevPicOrderCntLsb - sh->slice_pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2))) |
| 166 | PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb; |
| 167 | else if ((sh->slice_pic_order_cnt_lsb > prevPicOrderCntLsb) && |
| 168 | ((sh->slice_pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2))) |
| 169 | PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb; |
| 170 | else |
| 171 | PicOrderCntMsb = prevPicOrderCntMsb; |
| 172 | } |
| 173 | poc->PicOrderCntVal = PicOrderCntMsb + sh->slice_pic_order_cnt_lsb; |
| 174 | } else { |
| 175 | if (nalu_type == EVC_IDR_NUT) { |
| 176 | poc->PicOrderCntVal = 0; |
| 177 | poc->DocOffset = -1; |
| 178 | } else { |
Michael Niedermayer | d35eecd | 2023-10-04 22:10:18 | [diff] [blame] | 179 | int SubGopLength = 1 << sps->log2_sub_gop_length; |
| 180 | |
Michael Niedermayer | 68cc174 | 2023-10-04 22:19:53 | [diff] [blame] | 181 | if (tid > (SubGopLength > 1 ? 1 + av_log2(SubGopLength - 1) : 0)) |
| 182 | return AVERROR_INVALIDDATA; |
| 183 | |
James Almer | ff7a4cd | 2023-06-17 20:18:04 | [diff] [blame] | 184 | if (tid == 0) { |
| 185 | poc->PicOrderCntVal = poc->prevPicOrderCntVal + SubGopLength; |
| 186 | poc->DocOffset = 0; |
| 187 | poc->prevPicOrderCntVal = poc->PicOrderCntVal; |
| 188 | } else { |
| 189 | int ExpectedTemporalId; |
| 190 | int PocOffset; |
| 191 | int prevDocOffset = poc->DocOffset; |
| 192 | |
| 193 | poc->DocOffset = (prevDocOffset + 1) % SubGopLength; |
| 194 | if (poc->DocOffset == 0) { |
| 195 | poc->prevPicOrderCntVal += SubGopLength; |
| 196 | ExpectedTemporalId = 0; |
| 197 | } else |
Michael Niedermayer | d35eecd | 2023-10-04 22:10:18 | [diff] [blame] | 198 | ExpectedTemporalId = 1 + av_log2(poc->DocOffset); |
| 199 | |
James Almer | ff7a4cd | 2023-06-17 20:18:04 | [diff] [blame] | 200 | while (tid != ExpectedTemporalId) { |
| 201 | poc->DocOffset = (poc->DocOffset + 1) % SubGopLength; |
| 202 | if (poc->DocOffset == 0) |
| 203 | ExpectedTemporalId = 0; |
| 204 | else |
Michael Niedermayer | d35eecd | 2023-10-04 22:10:18 | [diff] [blame] | 205 | ExpectedTemporalId = 1 + av_log2(poc->DocOffset); |
James Almer | ff7a4cd | 2023-06-17 20:18:04 | [diff] [blame] | 206 | } |
Michael Niedermayer | d35eecd | 2023-10-04 22:10:18 | [diff] [blame] | 207 | PocOffset = (int)(SubGopLength * ((2.0 * poc->DocOffset + 1) / (1 << tid) - 2)); |
James Almer | ff7a4cd | 2023-06-17 20:18:04 | [diff] [blame] | 208 | poc->PicOrderCntVal = poc->prevPicOrderCntVal + PocOffset; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |