@@ -169,17 +169,6 @@ void VideoDecoder::initializeDecoder() {
169169 }
170170 containerMetadata_.numVideoStreams ++;
171171 } else if (avStream->codecpar ->codec_type == AVMEDIA_TYPE_AUDIO) {
172- int numSamplesPerFrame = avStream->codecpar ->frame_size ;
173- int sampleRate = avStream->codecpar ->sample_rate ;
174- if (numSamplesPerFrame > 0 && sampleRate > 0 ) {
175- // This should allow the approximate mode to do its magic.
176- // fps is numFrames / duration where
177- // - duration = numSamplesTotal / sampleRate and
178- // - numSamplesTotal = numSamplesPerFrame * numFrames
179- // so fps = numFrames * sampleRate / (numSamplesPerFrame * numFrames)
180- streamMetadata.averageFps =
181- static_cast <double >(sampleRate) / numSamplesPerFrame;
182- }
183172 containerMetadata_.numAudioStreams ++;
184173 }
185174
@@ -422,7 +411,7 @@ VideoDecoder::VideoStreamOptions::VideoStreamOptions(
422411void VideoDecoder::addStream (
423412 int streamIndex,
424413 AVMediaType mediaType,
425- const VideoStreamOptions& videoStreamOptions ) {
414+ const torch::Device& device ) {
426415 TORCH_CHECK (
427416 activeStreamIndex_ == NO_ACTIVE_STREAM,
428417 " Can only add one single stream." );
@@ -457,36 +446,25 @@ void VideoDecoder::addStream(
457446 activeStreamIndex_,
458447 " which is of the wrong media type." );
459448
460- // TODO_CODE_QUALITY this is meh to have that in the middle
461- if (mediaType == AVMEDIA_TYPE_VIDEO &&
462- videoStreamOptions. device .type () == torch::kCUDA ) {
449+ // TODO_CODE_QUALITY it's pretty meh to have a video-specific logic within
450+ // addStream() which is supposed to be generic
451+ if (mediaType == AVMEDIA_TYPE_VIDEO && device.type () == torch::kCUDA ) {
463452 avCodec = makeAVCodecOnlyUseForCallingAVFindBestStream (
464- findCudaCodec (
465- videoStreamOptions.device , streamInfo.stream ->codecpar ->codec_id )
453+ findCudaCodec (device, streamInfo.stream ->codecpar ->codec_id )
466454 .value_or (avCodec));
467455 }
468456
469457 AVCodecContext* codecContext = avcodec_alloc_context3 (avCodec);
470458 TORCH_CHECK (codecContext != nullptr );
471- codecContext->thread_count =
472- videoStreamOptions.ffmpegThreadCount .value_or (0 ); // TODO VIDEO ONLY?
473459 streamInfo.codecContext .reset (codecContext);
474460
475461 int retVal = avcodec_parameters_to_context (
476462 streamInfo.codecContext .get (), streamInfo.stream ->codecpar );
477463 TORCH_CHECK_EQ (retVal, AVSUCCESS);
478464
479- // TODO_CODE_QUALITY meh again
480- if (mediaType == AVMEDIA_TYPE_VIDEO) {
481- if (videoStreamOptions.device .type () == torch::kCPU ) {
482- // No more initialization needed for CPU.
483- } else if (videoStreamOptions.device .type () == torch::kCUDA ) {
484- initializeContextOnCuda (videoStreamOptions.device , codecContext);
485- } else {
486- TORCH_CHECK (
487- false , " Invalid device type: " + videoStreamOptions.device .str ());
488- }
489- streamInfo.videoStreamOptions = videoStreamOptions;
465+ // TODO_CODE_QUALITY same as above.
466+ if (mediaType == AVMEDIA_TYPE_VIDEO && device.type () == torch::kCUDA ) {
467+ initializeContextOnCuda (device, codecContext);
490468 }
491469
492470 retVal = avcodec_open2 (streamInfo.codecContext .get (), avCodec, nullptr );
@@ -512,9 +490,16 @@ void VideoDecoder::addStream(
512490void VideoDecoder::addVideoStream (
513491 int streamIndex,
514492 const VideoStreamOptions& videoStreamOptions) {
515- addStream (streamIndex, AVMEDIA_TYPE_VIDEO, videoStreamOptions);
493+ TORCH_CHECK (
494+ videoStreamOptions.device .type () == torch::kCPU ||
495+ videoStreamOptions.device .type () == torch::kCUDA ,
496+ " Invalid device type: " + videoStreamOptions.device .str ());
497+ addStream (streamIndex, AVMEDIA_TYPE_VIDEO, videoStreamOptions.device );
516498
517499 auto & streamInfo = streamInfos_[activeStreamIndex_];
500+ streamInfo.codecContext ->thread_count =
501+ videoStreamOptions.ffmpegThreadCount .value_or (0 );
502+
518503 containerMetadata_.allStreamMetadata [activeStreamIndex_].width =
519504 streamInfo.codecContext ->width ;
520505 containerMetadata_.allStreamMetadata [activeStreamIndex_].height =
@@ -547,8 +532,12 @@ void VideoDecoder::addAudioStream(int streamIndex) {
547532
548533 addStream (streamIndex, AVMEDIA_TYPE_AUDIO);
549534
550- containerMetadata_.allStreamMetadata [activeStreamIndex_].sampleRate =
551- streamInfo.codecContext ->sample_rate ;
535+ auto & streamInfo = streamInfos_[activeStreamIndex_];
536+ auto & streamMetadata =
537+ containerMetadata_.allStreamMetadata [activeStreamIndex_];
538+ streamMetadata.sampleRate =
539+ static_cast <int64_t >(streamInfo.codecContext ->sample_rate );
540+ streamMetadata.numChannels = getNumChannels (streamInfo.codecContext );
552541}
553542
554543// --------------------------------------------------------------------------
0 commit comments