Skip to content

Commit 0fce50b

Browse files
committed
bmdplay: Convert to AVCodecParameters
1 parent 2c7c5bb commit 0fce50b

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

bmdplay.cpp

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ IDeckLinkConfiguration *deckLinkConfiguration;
4949

5050
AVFormatContext *ic;
5151
AVFrame *avframe;
52-
AVStream *audio_st = NULL;
53-
AVStream *video_st = NULL;
52+
53+
typedef struct PlayStream {
54+
AVStream *st;
55+
AVCodecContext *codec;
56+
} PlayStream;
57+
58+
PlayStream audio;
59+
PlayStream video;
5460

5561
static enum AVPixelFormat pix_fmt = AV_PIX_FMT_UYVY422;
5662
static BMDPixelFormat pix = bmdFormat8BitYUV;
@@ -204,14 +210,14 @@ void *fill_queues(void *unused)
204210
videoqueue.size);
205211
}
206212
st = ic->streams[pkt.stream_index];
207-
switch (st->codec->codec_type) {
213+
switch (st->codecpar->codec_type) {
208214
case AVMEDIA_TYPE_VIDEO:
209215
if (pkt.pts != AV_NOPTS_VALUE) {
210216
if (first_pts == AV_NOPTS_VALUE) {
211217
first_pts = first_video_pts = pkt.pts;
212218
first_audio_pts =
213-
av_rescale_q(pkt.pts, video_st->time_base,
214-
audio_st->time_base);
219+
av_rescale_q(pkt.pts, video.st->time_base,
220+
audio.st->time_base);
215221
}
216222
pkt.pts -= first_video_pts;
217223
}
@@ -222,8 +228,8 @@ void *fill_queues(void *unused)
222228
if (first_pts == AV_NOPTS_VALUE) {
223229
first_pts = first_audio_pts = pkt.pts;
224230
first_video_pts =
225-
av_rescale_q(pkt.pts, audio_st->time_base,
226-
video_st->time_base);
231+
av_rescale_q(pkt.pts, audio.st->time_base,
232+
video.st->time_base);
227233
}
228234
pkt.pts -= first_audio_pts;
229235
}
@@ -374,41 +380,66 @@ int main(int argc, char *argv[])
374380
avformat_find_stream_info(ic, NULL);
375381

376382
for (int i = 0; i < ic->nb_streams; i++) {
377-
AVStream *st = ic->streams[i];
378-
AVCodecContext *avctx = st->codec;
379-
AVCodec *codec = avcodec_find_decoder(avctx->codec_id);
380-
if (!codec || avcodec_open2(avctx, codec, NULL) < 0)
381-
fprintf(
382-
stderr, "cannot find codecs for %s\n",
383-
(avctx->codec_type ==
384-
AVMEDIA_TYPE_AUDIO) ? "Audio" : "Video");
385-
if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
386-
audio_st = st;
387-
}
388-
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
389-
video_st = st;
383+
AVStream *st = ic->streams[i];
384+
AVCodecParameters *par = st->codecpar;
385+
AVCodec *codec = avcodec_find_decoder(par->codec_id);
386+
switch (par->codec_type) {
387+
case AVMEDIA_TYPE_AUDIO:
388+
case AVMEDIA_TYPE_VIDEO:
389+
if (codec) {
390+
AVCodecContext *avctx = avcodec_alloc_context3(codec);
391+
if (!avctx) {
392+
av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
393+
exit(1);
394+
}
395+
396+
if (avcodec_open2(avctx, codec, NULL) < 0) {
397+
avcodec_free_context(&avctx);
398+
av_log(NULL, AV_LOG_ERROR, "Codec open failed\n");
399+
exit(1);
400+
}
401+
402+
if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
403+
audio.st = st;
404+
audio.codec = avctx;
405+
}
406+
407+
if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
408+
video.st = st;
409+
video.codec = avctx;
410+
}
411+
} else {
412+
fprintf(
413+
stderr, "cannot find codecs for %s\n",
414+
(par->codec_type ==
415+
AVMEDIA_TYPE_AUDIO) ? "Audio" : "Video");
416+
continue;
417+
}
418+
break;
419+
default:
420+
av_log(NULL, AV_LOG_VERBOSE, "Skipping stream %d\n", i);
390421
}
391422
}
392423

393-
if (!audio_st) {
424+
if (!audio.st) {
394425
av_log(NULL, AV_LOG_ERROR,
395426
"No audio stream found - bmdplay will close now.\n");
396427
return 1;
397428
}
398429

399-
if (!video_st) {
430+
if (!video.st) {
400431
av_log(NULL, AV_LOG_ERROR,
401432
"No video stream found - bmdplay will close now.\n");
402433
return 1;
403434
}
404435

405436
av_dump_format(ic, 0, filename, 0);
406437

407-
sws = sws_getContext(video_st->codec->width,
408-
video_st->codec->height,
409-
video_st->codec->pix_fmt,
410-
video_st->codec->width,
411-
video_st->codec->height,
438+
sws = sws_getContext(video.st->codecpar->width,
439+
video.st->codecpar->height,
440+
(AVPixelFormat)video.st->codecpar->format,
441+
video.st->codecpar->width,
442+
video.st->codecpar->height,
412443
pix_fmt,
413444
SWS_BILINEAR, NULL, NULL, NULL);
414445

@@ -450,17 +481,17 @@ bool Player::Init(int videomode, int connection, int camera)
450481
}
451482

452483
m_audioSampleDepth =
453-
av_get_exact_bits_per_sample(audio_st->codec->codec_id);
484+
av_get_exact_bits_per_sample(audio.codec->codec_id);
454485

455-
switch (audio_st->codec->channels) {
486+
switch (audio.codec->channels) {
456487
case 2:
457488
case 8:
458489
case 16:
459490
break;
460491
default:
461492
fprintf(stderr,
462493
"%d channels not supported, please use 2, 8 or 16\n",
463-
audio_st->codec->channels);
494+
audio.codec->channels);
464495
goto bail;
465496
}
466497

@@ -619,7 +650,7 @@ void Player::StartRunning(int videomode)
619650
// Set the audio output mode
620651
if (m_deckLinkOutput->EnableAudioOutput(bmdAudioSampleRate48kHz,
621652
m_audioSampleDepth,
622-
audio_st->codec->channels,
653+
audio.codec->channels,
623654
bmdAudioOutputStreamTimestamped) !=
624655
S_OK) {
625656
fprintf(stderr, "Failed to enable audio output\n");
@@ -677,7 +708,7 @@ void Player::ScheduleNextFrame(bool prerolling)
677708
int got_picture;
678709
videoFrame->GetBytes(&frame);
679710

680-
avcodec_decode_video2(video_st->codec, avframe, &got_picture, &pkt);
711+
avcodec_decode_video2(video.codec, avframe, &got_picture, &pkt);
681712
if (got_picture) {
682713
avpicture_fill(&picture, (uint8_t *)frame, pix_fmt,
683714
m_frameWidth, m_frameHeight);
@@ -687,10 +718,10 @@ void Player::ScheduleNextFrame(bool prerolling)
687718

688719
if (m_deckLinkOutput->ScheduleVideoFrame(videoFrame,
689720
pkt.pts *
690-
video_st->time_base.num,
721+
video.st->time_base.num,
691722
pkt.duration *
692-
video_st->time_base.num,
693-
video_st->time_base.den) !=
723+
video.st->time_base.num,
724+
video.st->time_base.den) !=
694725
S_OK)
695726
fprintf(stderr, "Error scheduling frame\n");
696727
}
@@ -706,8 +737,8 @@ void Player::WriteNextAudioSamples()
706737
int got_frame = 0;
707738
int i;
708739
int bytes_per_sample =
709-
av_get_bytes_per_sample(audio_st->codec->sample_fmt) *
710-
audio_st->codec->channels;
740+
av_get_bytes_per_sample(audio.codec->sample_fmt) *
741+
audio.codec->channels;
711742
int samples, off = 0;
712743

713744
m_deckLinkOutput->GetBufferedAudioSampleFrameCount(&bufferedSamples);
@@ -725,7 +756,7 @@ void Player::WriteNextAudioSamples()
725756
off * bytes_per_sample,
726757
samples,
727758
pkt.pts + off,
728-
audio_st->time_base.den / audio_st->time_base.num,
759+
audio.st->time_base.den / audio.st->time_base.num,
729760
&samplesWritten) != S_OK)
730761
fprintf(stderr, "error writing audio sample\n");
731762
samples -= samplesWritten;

0 commit comments

Comments
 (0)