27 #include <libavcodec/avcodec.h>
28 #include <libavformat/avformat.h>
29 #if USE_AVCODEC_GREATER_THAN_58_134
30 #include <libavcodec/bsf.h>
49 SEEK_MODE_EXACT_FRAME = 0,
50 SEEK_MODE_PREV_KEY_FRAME = 1,
59 SEEK_CRITERIA_FRAME_NUM = 0,
60 SEEK_CRITERIA_TIME_STAMP = 1,
77 : use_seek_(
false), seek_frame_(0), seek_mode_(SEEK_MODE_PREV_KEY_FRAME), seek_crit_(SEEK_CRITERIA_FRAME_NUM),
78 out_frame_pts_(0), out_frame_duration_(0), num_frames_decoded_(0U) {}
81 : use_seek_(
true), seek_frame_(frame_id), seek_mode_(SEEK_MODE_PREV_KEY_FRAME),
82 seek_crit_(SEEK_CRITERIA_FRAME_NUM), out_frame_pts_(0), out_frame_duration_(0), num_frames_decoded_(0U) {}
85 use_seek_ = other.use_seek_;
86 seek_frame_ = other.seek_frame_;
87 seek_mode_ = other.seek_mode_;
88 seek_crit_ = other.seek_crit_;
89 out_frame_pts_ = other.out_frame_pts_;
90 out_frame_duration_ = other.out_frame_duration_;
91 num_frames_decoded_ = other.num_frames_decoded_;
102 uint64_t seek_frame_;
111 int64_t out_frame_pts_;
114 int64_t out_frame_duration_;
117 uint64_t num_frames_decoded_;
128 virtual int GetData(uint8_t *buf,
int buf_size) = 0;
129 virtual size_t GetBufferSize() = 0;
131 AVCodecID GetCodecID() {
return av_video_codec_id_; };
133 VideoDemuxer(StreamProvider *stream_provider) :
VideoDemuxer(CreateFmtContextUtil(stream_provider)) {av_io_ctx_ = av_fmt_input_ctx_->pb;}
135 if (!av_fmt_input_ctx_) {
139 av_packet_free(&packet_);
141 if (packet_filtered_) {
142 av_packet_free(&packet_filtered_);
145 av_bsf_free(&av_bsf_ctx_);
147 avformat_close_input(&av_fmt_input_ctx_);
149 av_freep(&av_io_ctx_->buffer);
150 av_freep(&av_io_ctx_);
152 if (data_with_header_) {
153 av_free(data_with_header_);
156 bool Demux(uint8_t **video,
int *video_size, int64_t *pts =
nullptr) {
157 if (!av_fmt_input_ctx_) {
162 av_packet_unref(packet_);
165 while ((ret = av_read_frame(av_fmt_input_ctx_, packet_)) >= 0 && packet_->stream_index != av_stream_) {
166 av_packet_unref(packet_);
171 if (is_h264_ || is_hevc_) {
172 if (packet_filtered_->data) {
173 av_packet_unref(packet_filtered_);
175 if (av_bsf_send_packet(av_bsf_ctx_, packet_) != 0) {
176 std::cerr <<
"ERROR: av_bsf_send_packet failed!" << std::endl;
179 if (av_bsf_receive_packet(av_bsf_ctx_, packet_filtered_) != 0) {
180 std::cerr <<
"ERROR: av_bsf_receive_packet failed!" << std::endl;
183 *video = packet_filtered_->data;
184 *video_size = packet_filtered_->size;
185 if (packet_filtered_->dts != AV_NOPTS_VALUE) {
186 pkt_dts_ = packet_filtered_->dts;
188 pkt_dts_ = packet_filtered_->pts;
191 *pts = (int64_t) (packet_filtered_->pts * default_time_scale_ * time_base_);
192 pkt_duration_ = packet_filtered_->duration;
195 if (is_mpeg4_ && (frame_count_ == 0)) {
196 int ext_data_size = av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata_size;
197 if (ext_data_size > 0) {
198 data_with_header_ = (uint8_t *)av_malloc(ext_data_size + packet_->size - 3 *
sizeof(uint8_t));
199 if (!data_with_header_) {
200 std::cerr <<
"ERROR: av_malloc failed!" << std::endl;
203 memcpy(data_with_header_, av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata, ext_data_size);
204 memcpy(data_with_header_ + ext_data_size, packet_->data + 3, packet_->size - 3 *
sizeof(uint8_t));
205 *video = data_with_header_;
206 *video_size = ext_data_size + packet_->size - 3 *
sizeof(uint8_t);
209 *video = packet_->data;
210 *video_size = packet_->size;
212 if (packet_->dts != AV_NOPTS_VALUE) {
213 pkt_dts_ = packet_->dts;
215 pkt_dts_ = packet_->pts;
218 *pts = (int64_t)(packet_->pts * default_time_scale_ * time_base_);
219 pkt_duration_ = packet_->duration;
225 bool Seek(
VideoSeekContext& seek_ctx, uint8_t** pp_video,
int* video_size) {
234 std::cerr <<
"ERROR: Seek isn't supported for this input." << std::endl;
238 if (IsVFR() && (SEEK_CRITERIA_FRAME_NUM == seek_ctx.seek_crit_)) {
239 std::cerr <<
"ERROR: Can't seek by frame number in VFR sequences. Seek by timestamp instead." << std::endl;
245 bool seek_backward =
true;
246 int64_t timestamp = 0;
249 switch (seek_ctx.seek_crit_) {
250 case SEEK_CRITERIA_FRAME_NUM:
251 timestamp = TsFromFrameNumber(seek_ctx.seek_frame_);
252 ret = av_seek_frame(av_fmt_input_ctx_, av_stream_, timestamp, seek_backward ? AVSEEK_FLAG_BACKWARD | flags : flags);
254 case SEEK_CRITERIA_TIME_STAMP:
255 timestamp = TsFromTime(seek_ctx.seek_frame_);
256 ret = av_seek_frame(av_fmt_input_ctx_, av_stream_, timestamp, seek_backward ? AVSEEK_FLAG_BACKWARD | flags : flags);
259 std::cerr <<
"ERROR: Invalid seek mode" << std::endl;
264 throw std::runtime_error(
"ERROR: seeking for frame");
270 int64_t target_ts = 0;
272 switch (seek_ctx.seek_crit_) {
273 case SEEK_CRITERIA_FRAME_NUM:
274 target_ts = TsFromFrameNumber(seek_ctx.seek_frame_);
276 case SEEK_CRITERIA_TIME_STAMP:
277 target_ts = TsFromTime(seek_ctx.seek_frame_);
280 std::cerr <<
"ERROR::Invalid seek criteria" << std::endl;
284 if (pkt_dts_ == target_ts) {
286 }
else if (pkt_dts_ > target_ts) {
298 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
302 if (!Demux(pp_video, video_size, &pkt_data.pts)) {
303 throw std::runtime_error(
"ERROR: Demux failed trying to seek for specified frame number/timestamp");
305 seek_done = is_seek_done(pkt_data, seek_ctx);
308 if ((tmp_ctx.seek_frame_--) >= 0) {
309 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
311 }
else if (seek_done < 0) {
312 tmp_ctx.seek_frame_++;
313 seek_frame(tmp_ctx, AVSEEK_FLAG_ANY);
315 if (tmp_ctx.seek_frame_ == seek_ctx.seek_frame_)
317 }
while (seek_done != 0);
319 seek_ctx.out_frame_pts_ = pkt_data.pts;
320 seek_ctx.out_frame_duration_ = pkt_data.duration = pkt_duration_;
325 seek_frame(seek_ctx, AVSEEK_FLAG_BACKWARD);
326 Demux(pp_video, video_size, &pkt_data.pts);
327 seek_ctx.num_frames_decoded_ =
static_cast<uint64_t
>(pkt_data.pts / 1000 * frame_rate_);
328 seek_ctx.out_frame_pts_ = pkt_data.pts;
329 seek_ctx.out_frame_duration_ = pkt_data.duration = pkt_duration_;
333 pktData.bsl_data = size_t(*pp_video);
334 pktData.bsl = *video_size;
336 switch (seek_ctx.seek_mode_) {
337 case SEEK_MODE_EXACT_FRAME:
338 seek_for_exact_frame(pktData, seek_ctx);
340 case SEEK_MODE_PREV_KEY_FRAME:
341 seek_for_prev_key_frame(pktData, seek_ctx);
344 throw std::runtime_error(
"ERROR::Unsupported seek mode");
350 const uint32_t GetWidth()
const {
return width_;}
351 const uint32_t GetHeight()
const {
return height_;}
352 const uint32_t GetChromaHeight()
const {
return chroma_height_;}
353 const uint32_t GetBitDepth()
const {
return bit_depth_;}
354 const uint32_t GetBytePerPixel()
const {
return byte_per_pixel_;}
355 const uint32_t GetBitRate()
const {
return bit_rate_;}
356 const double GetFrameRate()
const {
return frame_rate_;};
357 bool IsVFR()
const {
return frame_rate_ != avg_frame_rate_; };
358 int64_t TsFromTime(
double ts_sec) {
360 auto const ts_tbu = llround(ts_sec * AV_TIME_BASE);
362 AVRational time_factor = {1, AV_TIME_BASE};
363 return av_rescale_q(ts_tbu, time_factor, av_fmt_input_ctx_->streams[av_stream_]->time_base);
366 int64_t TsFromFrameNumber(int64_t frame_num) {
367 auto const ts_sec =
static_cast<double>(frame_num) / frame_rate_;
368 return TsFromTime(ts_sec);
372 VideoDemuxer(AVFormatContext *av_fmt_input_ctx) : av_fmt_input_ctx_(av_fmt_input_ctx) {
373 av_log_set_level(AV_LOG_QUIET);
374 if (!av_fmt_input_ctx_) {
375 std::cerr <<
"ERROR: av_fmt_input_ctx_ is not vaild!" << std::endl;
378 packet_ = av_packet_alloc();
379 packet_filtered_ = av_packet_alloc();
380 if (!packet_ || !packet_filtered_) {
381 std::cerr <<
"ERROR: av_packet_alloc failed!" << std::endl;
384 if (avformat_find_stream_info(av_fmt_input_ctx_,
nullptr) < 0) {
385 std::cerr <<
"ERROR: avformat_find_stream_info failed!" << std::endl;
388 av_stream_ = av_find_best_stream(av_fmt_input_ctx_, AVMEDIA_TYPE_VIDEO, -1, -1,
nullptr, 0);
389 if (av_stream_ < 0) {
390 std::cerr <<
"ERROR: av_find_best_stream failed!" << std::endl;
391 av_packet_free(&packet_);
392 av_packet_free(&packet_filtered_);
395 av_video_codec_id_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->codec_id;
396 width_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->width;
397 height_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->height;
398 chroma_format_ = (AVPixelFormat)av_fmt_input_ctx_->streams[av_stream_]->codecpar->format;
399 bit_rate_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->bit_rate;
400 if (av_fmt_input_ctx_->streams[av_stream_]->r_frame_rate.den != 0)
401 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);
402 if (av_fmt_input_ctx_->streams[av_stream_]->avg_frame_rate.den != 0)
403 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);
405 switch (chroma_format_) {
406 case AV_PIX_FMT_YUV420P10LE:
407 case AV_PIX_FMT_GRAY10LE:
409 chroma_height_ = (height_ + 1) >> 1;
412 case AV_PIX_FMT_YUV420P12LE:
414 chroma_height_ = (height_ + 1) >> 1;
417 case AV_PIX_FMT_YUV444P10LE:
419 chroma_height_ = height_ << 1;
422 case AV_PIX_FMT_YUV444P12LE:
424 chroma_height_ = height_ << 1;
427 case AV_PIX_FMT_YUV444P:
429 chroma_height_ = height_ << 1;
432 case AV_PIX_FMT_YUV420P:
433 case AV_PIX_FMT_YUVJ420P:
434 case AV_PIX_FMT_YUVJ422P:
435 case AV_PIX_FMT_YUVJ444P:
436 case AV_PIX_FMT_GRAY8:
438 chroma_height_ = (height_ + 1) >> 1;
442 chroma_format_ = AV_PIX_FMT_YUV420P;
444 chroma_height_ = (height_ + 1) >> 1;
448 AVRational time_base = av_fmt_input_ctx_->streams[av_stream_]->time_base;
449 time_base_ = av_q2d(time_base);
451 is_h264_ = av_video_codec_id_ == AV_CODEC_ID_H264 && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
452 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
453 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
454 is_hevc_ = av_video_codec_id_ == AV_CODEC_ID_HEVC && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
455 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
456 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
457 is_mpeg4_ = av_video_codec_id_ == AV_CODEC_ID_MPEG4 && (!strcmp(av_fmt_input_ctx_->iformat->long_name,
"QuickTime / MOV")
458 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"FLV (Flash Video)")
459 || !strcmp(av_fmt_input_ctx_->iformat->long_name,
"Matroska / WebM"));
462 is_seekable_ = av_fmt_input_ctx_->iformat->read_seek || av_fmt_input_ctx_->iformat->read_seek2;
465 const AVBitStreamFilter *bsf = av_bsf_get_by_name(
"h264_mp4toannexb");
467 std::cerr <<
"ERROR: av_bsf_get_by_name() failed" << std::endl;
468 av_packet_free(&packet_);
469 av_packet_free(&packet_filtered_);
472 if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0) {
473 std::cerr <<
"ERROR: av_bsf_alloc failed!" << std::endl;
476 avcodec_parameters_copy(av_bsf_ctx_->par_in, av_fmt_input_ctx_->streams[av_stream_]->codecpar);
477 if (av_bsf_init(av_bsf_ctx_) < 0) {
478 std::cerr <<
"ERROR: av_bsf_init failed!" << std::endl;
483 const AVBitStreamFilter *bsf = av_bsf_get_by_name(
"hevc_mp4toannexb");
485 std::cerr <<
"ERROR: av_bsf_get_by_name() failed" << std::endl;
486 av_packet_free(&packet_);
487 av_packet_free(&packet_filtered_);
490 if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0 ) {
491 std::cerr <<
"ERROR: av_bsf_alloc failed!" << std::endl;
494 avcodec_parameters_copy(av_bsf_ctx_->par_in, av_fmt_input_ctx_->streams[av_stream_]->codecpar);
495 if (av_bsf_init(av_bsf_ctx_) < 0) {
496 std::cerr <<
"ERROR: av_bsf_init failed!" << std::endl;
501 AVFormatContext *CreateFmtContextUtil(StreamProvider *stream_provider) {
502 AVFormatContext *ctx =
nullptr;
503 if (!(ctx = avformat_alloc_context())) {
504 std::cerr <<
"ERROR: avformat_alloc_context failed" << std::endl;
507 uint8_t *avioc_buffer =
nullptr;
508 int avioc_buffer_size = stream_provider->GetBufferSize();
509 avioc_buffer = (uint8_t *)av_malloc(avioc_buffer_size);
511 std::cerr <<
"ERROR: av_malloc failed!" << std::endl;
514 av_io_ctx_ = avio_alloc_context(avioc_buffer, avioc_buffer_size,
515 0, stream_provider, &ReadPacket,
nullptr,
nullptr);
517 std::cerr <<
"ERROR: avio_alloc_context failed!" << std::endl;
520 ctx->pb = av_io_ctx_;
522 if (avformat_open_input(&ctx,
nullptr,
nullptr,
nullptr) != 0) {
523 std::cerr <<
"ERROR: avformat_open_input failed!" << std::endl;
528 AVFormatContext *CreateFmtContextUtil(
const char *input_file_path) {
529 avformat_network_init();
530 AVFormatContext *ctx =
nullptr;
531 if (avformat_open_input(&ctx, input_file_path,
nullptr,
nullptr) != 0 ) {
532 std::cerr <<
"ERROR: avformat_open_input failed!" << std::endl;
537 static int ReadPacket(
void *data, uint8_t *buf,
int buf_size) {
538 return ((StreamProvider *)data)->GetData(buf, buf_size);
540 AVFormatContext *av_fmt_input_ctx_ =
nullptr;
541 AVIOContext *av_io_ctx_ =
nullptr;
542 AVPacket* packet_ =
nullptr;
543 AVPacket* packet_filtered_ =
nullptr;
544 AVBSFContext *av_bsf_ctx_ =
nullptr;
545 AVCodecID av_video_codec_id_;
546 AVPixelFormat chroma_format_;
547 double frame_rate_ = 0.0;
548 double avg_frame_rate_ = 0.0;
549 uint8_t *data_with_header_ =
nullptr;
551 bool is_h264_ =
false;
552 bool is_hevc_ =
false;
553 bool is_mpeg4_ =
false;
554 bool is_seekable_ =
false;
555 int64_t default_time_scale_ = 1000;
556 double time_base_ = 0.0;
557 uint32_t frame_count_ = 0;
559 uint32_t height_ = 0;
560 uint32_t chroma_height_ = 0;
561 uint32_t bit_depth_ = 0;
562 uint32_t byte_per_pixel_ = 0;
563 uint32_t bit_rate_ = 0;
565 int64_t pkt_dts_ = 0;
566 int64_t pkt_duration_ = 0;
569 static inline rocDecVideoCodec AVCodec2RocDecVideoCodec(AVCodecID av_codec) {
Definition: video_demuxer.h:125
Definition: video_demuxer.h:123
Definition: video_demuxer.h:74
The AMD rocDecode Library.
@ rocDecVideoCodec_AV1
Definition: rocdecode.h:84
@ rocDecVideoCodec_MPEG4
Definition: rocdecode.h:81
@ rocDecVideoCodec_HEVC
Definition: rocdecode.h:83
@ rocDecVideoCodec_MPEG1
Definition: rocdecode.h:79
@ rocDecVideoCodec_JPEG
Definition: rocdecode.h:87
@ rocDecVideoCodec_VP9
Definition: rocdecode.h:86
@ rocDecVideoCodec_AVC
Definition: rocdecode.h:82
@ rocDecVideoCodec_MPEG2
Definition: rocdecode.h:80
@ rocDecVideoCodec_VP8
Definition: rocdecode.h:85
@ rocDecVideoCodec_NumCodecs
Definition: rocdecode.h:88
Definition: video_demuxer.h:64
SeekModeEnum
Enum for Seek mode.
Definition: video_demuxer.h:48
enum SeekModeEnum SeekMode
Enum for Seek mode.
SeekCriteriaEnum
Enum for Seek Criteria.
Definition: video_demuxer.h:58
enum SeekCriteriaEnum SeekCriteria
Enum for Seek Criteria.