]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/hevc.c
fate: Add hap-chunk ref file
[ffmpeg] / libavformat / hevc.c
index ab4eb10a8b7820639e430a57709b6514603810a5..e59d6a8886c8709e1984bea39d7b3907fe0aab7f 100644 (file)
@@ -402,10 +402,10 @@ static void skip_scaling_list_data(GetBitContext *gb)
                 num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
 
                 if (i > 1)
-                    get_se_golomb(gb); // scaling_list_dc_coef_minus8[i-2][j]
+                    get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
 
                 for (k = 0; k < num_coeffs; k++)
-                    get_se_golomb(gb); // scaling_list_delta_coef
+                    get_se_golomb_long(gb); // scaling_list_delta_coef
             }
 }
 
@@ -593,7 +593,7 @@ static int hvcc_parse_pps(GetBitContext *gb,
 
     get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
     get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
-    get_se_golomb     (gb); // init_qp_minus26
+    get_se_golomb_long(gb); // init_qp_minus26
 
     /*
      * constrained_intra_pred_flag u(1)
@@ -604,8 +604,8 @@ static int hvcc_parse_pps(GetBitContext *gb,
     if (get_bits1(gb))          // cu_qp_delta_enabled_flag
         get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
 
-    get_se_golomb(gb); // pps_cb_qp_offset
-    get_se_golomb(gb); // pps_cr_qp_offset
+    get_se_golomb_long(gb); // pps_cb_qp_offset
+    get_se_golomb_long(gb); // pps_cr_qp_offset
 
     /*
      * weighted_pred_flag               u(1)
@@ -636,7 +636,7 @@ static uint8_t *nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
     uint8_t *dst;
     uint32_t i, len;
 
-    dst = av_malloc(src_len);
+    dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!dst)
         return NULL;
 
@@ -849,51 +849,51 @@ static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
     hvcc->avgFrameRate      = 0;
     hvcc->constantFrameRate = 0;
 
-    av_dlog(NULL,  "configurationVersion:                %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "configurationVersion:                %"PRIu8"\n",
             hvcc->configurationVersion);
-    av_dlog(NULL,  "general_profile_space:               %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "general_profile_space:               %"PRIu8"\n",
             hvcc->general_profile_space);
-    av_dlog(NULL,  "general_tier_flag:                   %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "general_tier_flag:                   %"PRIu8"\n",
             hvcc->general_tier_flag);
-    av_dlog(NULL,  "general_profile_idc:                 %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "general_profile_idc:                 %"PRIu8"\n",
             hvcc->general_profile_idc);
-    av_dlog(NULL, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
+    av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
             hvcc->general_profile_compatibility_flags);
-    av_dlog(NULL, "general_constraint_indicator_flags:  0x%012"PRIx64"\n",
+    av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags:  0x%012"PRIx64"\n",
             hvcc->general_constraint_indicator_flags);
-    av_dlog(NULL,  "general_level_idc:                   %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "general_level_idc:                   %"PRIu8"\n",
             hvcc->general_level_idc);
-    av_dlog(NULL,  "min_spatial_segmentation_idc:        %"PRIu16"\n",
+    av_log(NULL, AV_LOG_TRACE,  "min_spatial_segmentation_idc:        %"PRIu16"\n",
             hvcc->min_spatial_segmentation_idc);
-    av_dlog(NULL,  "parallelismType:                     %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "parallelismType:                     %"PRIu8"\n",
             hvcc->parallelismType);
-    av_dlog(NULL,  "chromaFormat:                        %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "chromaFormat:                        %"PRIu8"\n",
             hvcc->chromaFormat);
-    av_dlog(NULL,  "bitDepthLumaMinus8:                  %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "bitDepthLumaMinus8:                  %"PRIu8"\n",
             hvcc->bitDepthLumaMinus8);
-    av_dlog(NULL,  "bitDepthChromaMinus8:                %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "bitDepthChromaMinus8:                %"PRIu8"\n",
             hvcc->bitDepthChromaMinus8);
-    av_dlog(NULL,  "avgFrameRate:                        %"PRIu16"\n",
+    av_log(NULL, AV_LOG_TRACE,  "avgFrameRate:                        %"PRIu16"\n",
             hvcc->avgFrameRate);
-    av_dlog(NULL,  "constantFrameRate:                   %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "constantFrameRate:                   %"PRIu8"\n",
             hvcc->constantFrameRate);
-    av_dlog(NULL,  "numTemporalLayers:                   %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "numTemporalLayers:                   %"PRIu8"\n",
             hvcc->numTemporalLayers);
-    av_dlog(NULL,  "temporalIdNested:                    %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "temporalIdNested:                    %"PRIu8"\n",
             hvcc->temporalIdNested);
-    av_dlog(NULL,  "lengthSizeMinusOne:                  %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "lengthSizeMinusOne:                  %"PRIu8"\n",
             hvcc->lengthSizeMinusOne);
-    av_dlog(NULL,  "numOfArrays:                         %"PRIu8"\n",
+    av_log(NULL, AV_LOG_TRACE,  "numOfArrays:                         %"PRIu8"\n",
             hvcc->numOfArrays);
     for (i = 0; i < hvcc->numOfArrays; i++) {
-        av_dlog(NULL, "array_completeness[%"PRIu8"]:               %"PRIu8"\n",
+        av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]:               %"PRIu8"\n",
                 i, hvcc->array[i].array_completeness);
-        av_dlog(NULL, "NAL_unit_type[%"PRIu8"]:                    %"PRIu8"\n",
+        av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]:                    %"PRIu8"\n",
                 i, hvcc->array[i].NAL_unit_type);
-        av_dlog(NULL, "numNalus[%"PRIu8"]:                         %"PRIu16"\n",
+        av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]:                         %"PRIu16"\n",
                 i, hvcc->array[i].numNalus);
         for (j = 0; j < hvcc->array[i].numNalus; j++)
-            av_dlog(NULL,
+            av_log(NULL, AV_LOG_TRACE,
                     "nalUnitLength[%"PRIu8"][%"PRIu16"]:                 %"PRIu16"\n",
                     i, j, hvcc->array[i].nalUnitLength[j]);
     }
@@ -1014,6 +1014,70 @@ static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
     return 0;
 }
 
+int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
+                       int size, int filter_ps, int *ps_count)
+{
+    int num_ps = 0, ret = 0;
+    uint8_t *buf, *end, *start = NULL;
+
+    if (!filter_ps) {
+        ret = ff_avc_parse_nal_units(pb, buf_in, size);
+        goto end;
+    }
+
+    ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
+    if (ret < 0)
+        goto end;
+
+    ret = 0;
+    buf = start;
+    end = start + size;
+
+    while (end - buf > 4) {
+        uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
+        uint8_t type = (buf[4] >> 1) & 0x3f;
+
+        buf += 4;
+
+        switch (type) {
+        case NAL_VPS:
+        case NAL_SPS:
+        case NAL_PPS:
+            num_ps++;
+            break;
+        default:
+            ret += 4 + len;
+            avio_wb32(pb, len);
+            avio_write(pb, buf, len);
+            break;
+        }
+
+        buf += len;
+    }
+
+end:
+    av_free(start);
+    if (ps_count)
+        *ps_count = num_ps;
+    return ret;
+}
+
+int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
+                           int *size, int filter_ps, int *ps_count)
+{
+    AVIOContext *pb;
+    int ret;
+
+    ret = avio_open_dyn_buf(&pb);
+    if (ret < 0)
+        return ret;
+
+    ret   = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
+    *size = avio_close_dyn_buf(pb, buf_out);
+
+    return ret;
+}
+
 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
                        int size, int ps_array_completeness)
 {