Skip to content

Commit

Permalink
avcodec/utils: calculate frame number of HEVC if the framerate > 30FPS
Browse files Browse the repository at this point in the history
Signed-off-by: Limin Wang <[email protected]>
  • Loading branch information
lance-lmwang committed Aug 16, 2020
1 parent 6328a57 commit d7af6d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libavcodec/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx);
* Check AVFrame for S12M timecode side data and allocate and fill TC SEI message with timecode info
*
* @param frame Raw frame to get S12M timecode side data from
* @param rate The frame rate
* @param prefix_len Number of bytes to allocate before SEI message
* @param data Pointer to a variable to store allocated memory
* Upon return the variable will hold NULL on error or if frame has no S12M timecode info.
Expand All @@ -375,7 +376,7 @@ AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx);
* @param sei_size Pointer to a variable to store generated SEI message length
* @return Zero on success, negative error code on failure
*/
int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
void **data, size_t *sei_size);

/**
Expand Down
2 changes: 1 addition & 1 deletion libavcodec/nvenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
void *tc_data = NULL;
size_t tc_size = 0;

if (ff_alloc_timecode_sei(frame, 0, (void**)&tc_data, &tc_size) < 0) {
if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) {
av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
}

Expand Down
13 changes: 12 additions & 1 deletion libavcodec/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ static unsigned bcd2uint(uint8_t bcd)
return low + 10*high;
}

int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
void **data, size_t *sei_size)
{
AVFrameSideData *sd = NULL;
Expand Down Expand Up @@ -2249,6 +2249,17 @@ int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit

/* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
unsigned pc;
ff *= 2;
if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
pc = !!(tcsmpte & 1 << 7);
else
pc = !!(tcsmpte & 1 << 23);
ff = (ff + pc) & 0x7f;
}

put_bits(&pb, 1, 1); // clock_timestamp_flag
put_bits(&pb, 1, 1); // units_field_based_flag
put_bits(&pb, 5, 0); // counting_type
Expand Down

0 comments on commit d7af6d1

Please sign in to comment.