27 #include <libavcodec/avcodec.h>
28 #include <libavformat/avformat.h>
29 #if USE_AVCODEC_GREATER_THAN_58_134
30 #include <libavcodec/bsf.h>
48 SEEK_MODE_EXACT_FRAME = 0,
49 SEEK_MODE_PREV_KEY_FRAME = 1,
58 SEEK_CRITERIA_FRAME_NUM = 0,
59 SEEK_CRITERIA_TIME_STAMP = 1,
76 : use_seek_(
false), seek_frame_(0), seek_mode_(SEEK_MODE_PREV_KEY_FRAME), seek_crit_(SEEK_CRITERIA_FRAME_NUM),
77 out_frame_pts_(0), out_frame_duration_(0), num_frames_decoded_(0U) {}
80 : use_seek_(
true), seek_frame_(frame_id), seek_mode_(SEEK_MODE_PREV_KEY_FRAME),
81 seek_crit_(SEEK_CRITERIA_FRAME_NUM), out_frame_pts_(0), out_frame_duration_(0), num_frames_decoded_(0U) {}
84 use_seek_ = other.use_seek_;
85 seek_frame_ = other.seek_frame_;
86 seek_mode_ = other.seek_mode_;
87 seek_crit_ = other.seek_crit_;
88 out_frame_pts_ = other.out_frame_pts_;
89 out_frame_duration_ = other.out_frame_duration_;
90 num_frames_decoded_ = other.num_frames_decoded_;
101 uint64_t seek_frame_;
110 int64_t out_frame_pts_;
113 int64_t out_frame_duration_;
116 uint64_t num_frames_decoded_;
127 virtual int GetData(uint8_t *buf,
int buf_size) = 0;
129 AVCodecID GetCodecID() {
return av_video_codec_id_; };
131 VideoDemuxer(StreamProvider *stream_provider) :
VideoDemuxer(CreateFmtContextUtil(stream_provider)) {av_io_ctx_ = av_fmt_input_ctx_->pb;}
133 if (!av_fmt_input_ctx_) {
137 av_packet_free(&packet_);
139 if (packet_filtered_) {
140 av_packet_free(&packet_filtered_);
143 av_bsf_free(&av_bsf_ctx_);
145 avformat_close_input(&av_fmt_input_ctx_);
147 av_freep(&av_io_ctx_->buffer);
148 av_freep(&av_io_ctx_);
150 if (data_with_header_) {
151 av_free(data_with_header_);
154 bool Demux(uint8_t **video,
int *video_size, int64_t *pts =
nullptr) {
155 if (!av_fmt_input_ctx_) {
160 av_packet_unref(packet_);
163 while ((ret = av_read_frame(av_fmt_input_ctx_, packet_)) >= 0 && packet_->stream_index != av_stream_) {
164 av_packet_unref(packet_);
169 if (is_h264_ || is_hevc_) {
170 if (packet_filtered_->data) {
171 av_packet_unref(packet_filtered_);
173 if (av_bsf_send_packet(av_bsf_ctx_, packet_) != 0) {
174 std::cerr <<
"ERROR: av_bsf_send_packet failed!" << std::endl;
177 if (av_bsf_receive_packet(av_bsf_ctx_, packet_filtered_) != 0) {
178 std::cerr <<
"ERROR: av_bsf_receive_packet failed!" << std::endl;
181 *video = packet_filtered_->data;
182 *video_size = packet_filtered_->size;
183 if (packet_filtered_->dts != AV_NOPTS_VALUE) {
184 pkt_dts_ = packet_filtered_->dts;
186 pkt_dts_ = packet_filtered_->pts;
189 *pts = (int64_t) (packet_filtered_->pts * default_time_scale_ * time_base_);
190 pkt_duration_ = packet_filtered_->duration;
193 if (is_mpeg4_ && (frame_count_ == 0)) {
194 int ext_data_size = av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata_size;
195 if (ext_data_size > 0) {
196 data_with_header_ = (uint8_t *)av_malloc(ext_data_size + packet_->size - 3 *
sizeof(uint8_t));
197 if (!data_with_header_) {
198 std::cerr <<
"ERROR: av_malloc failed!" << std::endl;
201 memcpy(data_with_header_, av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata, ext_data_size);
202 memcpy(data_with_header_ + ext_data_size, packet_->data + 3, packet_->size - 3 *
sizeof(uint8_t));
203 *video = data_with_header_;
204 *video_size = ext_data_size + packet_->size - 3 *
sizeof(uint8_t);
207 *video = packet_->data;
208 *video_size = packet_->size;
210 if (packet_->dts != AV_NOPTS_VALUE) {
211 pkt_dts_ = packet_->dts;
213 pkt_dts_ = packet_->pts;
216 *pts = (int64_t)(packet_->pts * default_time_scale_ * time_base_);
217 pkt_duration_ = packet_->duration;
223 bool Seek(
VideoSeekContext& seek_ctx, uint8_t** pp_video,
int* video_size) {
232 std::cerr <<
"ERROR: Seek isn't supported for this input." << std::endl;
236 if (IsVFR() && (SEEK_CRITERIA_FRAME_NUM == seek_ctx.seek_crit_)) {
237 std::cerr <<
"ERROR: Can't seek by frame number in VFR sequences. Seek by timestamp instead." << std::endl;
243 bool seek_backward =
true;
244 int64_t timestamp = 0;
247 switch (seek_ctx.seek_crit_) {
248 case SEEK_CRITERIA_FRAME_NUM:
249 timestamp = TsFromFrameNumber(seek_ctx.seek_frame_);
250 ret = av_seek_frame(av_fmt_input_ctx_, av_stream_, timestamp, seek_backward ? AVSEEK_FLAG_BACKWARD | flags : flags);
252 case SEEK_CRITERIA_TIME_STAMP:
253 timestamp = TsFromTime(seek_ctx.seek_frame_);
254 ret = av_seek_frame(av_fmt_input_ctx_, av_stream_, timestamp, seek_backward ? AVSEEK_FLAG_BACKWARD | flags : flags);
257 std::cerr <<
"ERROR: Invalid seek mode" << std::endl;
262 throw std::runtime_error(
"ERROR: seeking for frame");
268 int64_t target_ts = 0;
270 switch (seek_ctx.seek_crit_) {
271 case SEEK_CRITERIA_FRAME_NUM:
272 target_ts = TsFromFrameNumber(seek_ctx.seek_frame_);
274 case SEEK_CRITERIA_TIME_STAMP:
275 target_ts = TsFromTime(seek_ctx.seek_frame_);
278 std::cerr <<
"ERROR::Invalid seek criteria" << std::endl;
282 if (pkt_dts_ == target_ts) {
284 }
else if (pkt_dts_ > target_ts) {
296 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
300 if (!Demux(pp_video, video_size, &pkt_data.pts)) {
301 throw std::runtime_error(
"ERROR: Demux failed trying to seek for specified frame number/timestamp");
303 seek_done = is_seek_done(pkt_data, seek_ctx);
306 if ((tmp_ctx.seek_frame_--) >= 0) {
307 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
309 }
else if (seek_done < 0) {
310 tmp_ctx.seek_frame_++;
311 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
313 if (tmp_ctx.seek_frame_ == seek_ctx.seek_frame_)
315 }
while (seek_done != 0);
317 seek_ctx.out_frame_pts_ = pkt_data.pts;
318 seek_ctx.out_frame_duration_ = pkt_data.duration = pkt_duration_;
323 seek_frame(seek_ctx, AVSEEK_FLAG_BACKWARD);
324 Demux(pp_video, video_size, &pkt_data.pts);
325 seek_ctx.num_frames_decoded_ =
static_cast<uint64_t
>(pkt_data.pts / 1000 * frame_rate_);
326 seek_ctx.out_frame_pts_ = pkt_data.pts;
327 seek_ctx.out_frame_duration_ = pkt_data.duration = pkt_duration_;
331 pktData.bsl_data = size_t(*pp_video);
332 pktData.bsl = *video_size;
334 switch (seek_ctx.seek_mode_) {
335 case SEEK_MODE_EXACT_FRAME:
336 seek_for_exact_frame(pktData, seek_ctx);
338 case SEEK_MODE_PREV_KEY_FRAME:
339 seek_for_prev_key_frame(pktData, seek_ctx);
342 throw std::runtime_error(
"ERROR::Unsupported seek mode");
348 const uint32_t GetWidth()
const {
return width_;}
349 const uint32_t GetHeight()
const {
return height_;}
350 const uint32_t GetChromaHeight()
const {
return chroma_height_;}
351 const uint32_t GetBitDepth()
const {
return bit_depth_;}
352 const uint32_t GetBytePerPixel()
const {
return byte_per_pixel_;}
353 const uint32_t GetBitRate()
const {
return bit_rate_;}
354 const double GetFrameRate()
const {
return frame_rate_;};
355 bool IsVFR()
const {
return frame_rate_ != avg_frame_rate_; };
356 int64_t TsFromTime(
double ts_sec) {
358 auto const ts_tbu = llround(ts_sec * AV_TIME_BASE);
360 AVRational time_factor = {1, AV_TIME_BASE};
361 return av_rescale_q(ts_tbu, time_factor, av_fmt_input_ctx_->streams[av_stream_]->time_base);
364 int64_t TsFromFrameNumber(int64_t frame_num) {
365 auto const ts_sec =
static_cast<double>(frame_num) / frame_rate_;
366 return TsFromTime(ts_sec);
370 VideoDemuxer(AVFormatContext *av_fmt_input_ctx) : av_fmt_input_ctx_(av_fmt_input_ctx) {
371 av_log_set_level(AV_LOG_QUIET);
372 if (!av_fmt_input_ctx_) {
373 std::cerr <<
"ERROR: av_fmt_input_ctx_ is not vaild!" << std::endl;
376 packet_ = av_packet_alloc();
377 packet_filtered_ = av_packet_alloc();
378 if (!packet_ || !packet_filtered_) {
379 std::cerr <<
"ERROR: av_packet_alloc failed!" << std::endl;
382 if (avformat_find_stream_info(av_fmt_input_ctx_,
nullptr) < 0) {
383 std::cerr <<
"ERROR: avformat_find_stream_info failed!" << std::endl;
386 av_stream_ = av_find_best_stream(av_fmt_input_ctx_, AVMEDIA_TYPE_VIDEO, -1, -1,
nullptr, 0);
387 if (av_stream_ < 0) {
388 std::cerr <<
"ERROR: av_find_best_stream failed!" << std::endl;
389 av_packet_free(&packet_);
390 av_packet_free(&packet_filtered_);
393 av_video_codec_id_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->codec_id;
394 width_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->width;
395 height_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->height;
396 chroma_format_ = (AVPixelFormat)av_fmt_input_ctx_->streams[av_stream_]->codecpar->format;
397 bit_rate_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->bit_rate;
398 if (av_fmt_input_ctx_->streams[av_stream_]->r_frame_rate.den != 0)
399 frame_rate_ =
static_cast<double>(av_fmt_input_ctx_->streams[av_stream_]->r_frame_rate.num) /
static_cast<double>(av_fmt_input_ctx_->streams[av_stream_]->r_frame_rate.den);
400 if (av_fmt_input_ctx_->streams[av_stream_]->avg_frame_rate.den != 0)
401 avg_frame_rate_ =
static_cast<double>(av_fmt_input_ctx_->streams[av_stream_]->avg_frame_rate.num) /
static_cast<double>(av_fmt_input_ctx_->streams[av_stream_]->avg_frame_rate.den);
403 switch (chroma_format_) {
404 case AV_PIX_FMT_YUV420P10LE:
405 case AV_PIX_FMT_GRAY10LE:
407 chroma_height_ = (height_ + 1) >> 1;
410 case AV_PIX_FMT_YUV420P12LE:
412 chroma_height_ = (height_ + 1) >> 1;
415 case AV_PIX_FMT_YUV444P10LE:
417 chroma_height_ = height_ << 1;
420 case AV_PIX_FMT_YUV444P12LE:
422 chroma_height_ = height_ << 1;
425 case AV_PIX_FMT_YUV444P:
427 chroma_height_ = height_ << 1;
430 case AV_PIX_FMT_YUV420P:
431 case AV_PIX_FMT_YUVJ420P:
432 case AV_PIX_FMT_YUVJ422P:
433 case AV_PIX_FMT_YUVJ444P:
434 case AV_PIX_FMT_GRAY8:
436 chroma_height_ = (height_ + 1) >> 1;
440 chroma_format_ = AV_PIX_FMT_YUV420P;
442 chroma_height_ = (height_ + 1) >> 1;
446 AVRational time_base = av_fmt_input_ctx_->streams[av_stream_]->time_base;
447 time_base_ = av_q2d(time_base);
449 is_h264_ = av_video_codec_id_ == AV_CODEC_ID_H264 && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
450 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
451 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
452 is_hevc_ = av_video_codec_id_ == AV_CODEC_ID_HEVC && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
453 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
454 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
455 is_mpeg4_ = av_video_codec_id_ == AV_CODEC_ID_MPEG4 && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
456 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
457 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
460 is_seekable_ = av_fmt_input_ctx_->iformat->read_seek || av_fmt_input_ctx_->iformat->read_seek2;
463 const AVBitStreamFilter *bsf = av_bsf_get_by_name(
"h264_mp4toannexb");
465 std::cerr <<
"ERROR: av_bsf_get_by_name() failed" << std::endl;
466 av_packet_free(&packet_);
467 av_packet_free(&packet_filtered_);
470 if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0) {
471 std::cerr <<
"ERROR: av_bsf_alloc failed!" << std::endl;
474 avcodec_parameters_copy(av_bsf_ctx_->par_in, av_fmt_input_ctx_->streams[av_stream_]->codecpar);
475 if (av_bsf_init(av_bsf_ctx_) < 0) {
476 std::cerr <<
"ERROR: av_bsf_init failed!" << std::endl;
481 const AVBitStreamFilter *bsf = av_bsf_get_by_name(
"hevc_mp4toannexb");
483 std::cerr <<
"ERROR: av_bsf_get_by_name() failed" << std::endl;
484 av_packet_free(&packet_);
485 av_packet_free(&packet_filtered_);
488 if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0 ) {
489 std::cerr <<
"ERROR: av_bsf_alloc failed!" << std::endl;
492 avcodec_parameters_copy(av_bsf_ctx_->par_in, av_fmt_input_ctx_->streams[av_stream_]->codecpar);
493 if (av_bsf_init(av_bsf_ctx_) < 0) {
494 std::cerr <<
"ERROR: av_bsf_init failed!" << std::endl;
499 AVFormatContext *CreateFmtContextUtil(StreamProvider *stream_provider) {
500 AVFormatContext *ctx =
nullptr;
501 if (!(ctx = avformat_alloc_context())) {
502 std::cerr <<
"ERROR: avformat_alloc_context failed" << std::endl;
505 uint8_t *avioc_buffer =
nullptr;
506 int avioc_buffer_size = 100 * 1024 * 1024;
507 avioc_buffer = (uint8_t *)av_malloc(avioc_buffer_size);
509 std::cerr <<
"ERROR: av_malloc failed!" << std::endl;
512 av_io_ctx_ = avio_alloc_context(avioc_buffer, avioc_buffer_size,
513 0, stream_provider, &ReadPacket,
nullptr,
nullptr);
515 std::cerr <<
"ERROR: avio_alloc_context failed!" << std::endl;
518 ctx->pb = av_io_ctx_;
520 if (avformat_open_input(&ctx,
nullptr,
nullptr,
nullptr) != 0) {
521 std::cerr <<
"ERROR: avformat_open_input failed!" << std::endl;
526 AVFormatContext *CreateFmtContextUtil(
const char *input_file_path) {
527 avformat_network_init();
528 AVFormatContext *ctx =
nullptr;
529 if (avformat_open_input(&ctx, input_file_path,
nullptr,
nullptr) != 0 ) {
530 std::cerr <<
"ERROR: avformat_open_input failed!" << std::endl;
535 static int ReadPacket(
void *data, uint8_t *buf,
int buf_size) {
536 return ((StreamProvider *)data)->GetData(buf, buf_size);
538 AVFormatContext *av_fmt_input_ctx_ =
nullptr;
539 AVIOContext *av_io_ctx_ =
nullptr;
540 AVPacket* packet_ =
nullptr;
541 AVPacket* packet_filtered_ =
nullptr;
542 AVBSFContext *av_bsf_ctx_ =
nullptr;
543 AVCodecID av_video_codec_id_;
544 AVPixelFormat chroma_format_;
545 double frame_rate_ = 0.0;
546 double avg_frame_rate_ = 0.0;
547 uint8_t *data_with_header_ =
nullptr;
549 bool is_h264_ =
false;
550 bool is_hevc_ =
false;
551 bool is_mpeg4_ =
false;
552 bool is_seekable_ =
false;
553 int64_t default_time_scale_ = 1000;
554 double time_base_ = 0.0;
555 uint32_t frame_count_ = 0;
557 uint32_t height_ = 0;
558 uint32_t chroma_height_ = 0;
559 uint32_t bit_depth_ = 0;
560 uint32_t byte_per_pixel_ = 0;
561 uint32_t bit_rate_ = 0;
563 int64_t pkt_dts_ = 0;
564 int64_t pkt_duration_ = 0;
567 static inline rocDecVideoCodec AVCodec2RocDecVideoCodec(AVCodecID av_codec) {
Definition: video_demuxer.h:124
Definition: video_demuxer.h:122
Definition: video_demuxer.h:73
The AMD rocDecode Library.
@ rocDecVideoCodec_AV1
Definition: rocdecode.h:83
@ rocDecVideoCodec_MPEG4
Definition: rocdecode.h:80
@ rocDecVideoCodec_HEVC
Definition: rocdecode.h:82
@ rocDecVideoCodec_MPEG1
Definition: rocdecode.h:78
@ rocDecVideoCodec_JPEG
Definition: rocdecode.h:86
@ rocDecVideoCodec_VP9
Definition: rocdecode.h:85
@ rocDecVideoCodec_AVC
Definition: rocdecode.h:81
@ rocDecVideoCodec_MPEG2
Definition: rocdecode.h:79
@ rocDecVideoCodec_VP8
Definition: rocdecode.h:84
@ rocDecVideoCodec_NumCodecs
Definition: rocdecode.h:87
Definition: video_demuxer.h:63
SeekModeEnum
Enum for Seek mode.
Definition: video_demuxer.h:47
enum SeekModeEnum SeekMode
Enum for Seek mode.
SeekCriteriaEnum
Enum for Seek Criteria.
Definition: video_demuxer.h:57
enum SeekCriteriaEnum SeekCriteria
Enum for Seek Criteria.