try moving codec selection up above ctx allocation so that it allocates a priv_data

This commit is contained in:
Isaac Connor 2017-12-02 08:12:04 -05:00
parent 0ef9d13dbd
commit b1019267d8
2 changed files with 88 additions and 95 deletions

View File

@ -2047,7 +2047,7 @@ int LocalCamera::Capture( ZMPacket &zm_packet ) {
}
} else {
Debug( 3, "No format conversion performed. Assigning the image" );
Debug( 5, "No format conversion performed. Assigning the image" );
/* No conversion was performed, the image is in the V4L buffers and needs to be copied into the shared memory */
zm_packet.image->Assign(width, height, colours, subpixelorder, buffer, imagesize);

View File

@ -117,12 +117,31 @@ VideoStore::VideoStore(
video_in_stream_index = 0;
}
video_out_ctx = avcodec_alloc_context3(NULL);
if ( monitor->OutputCodec() == "mjpeg" ) {
video_out_codec = avcodec_find_encoder_by_name("mjpeg");
if ( ! video_out_codec ) {
Debug(1, "Didn't find omx");
video_out_codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
}
video_out_ctx->codec_id = video_out_codec->id;
video_out_ctx->pix_fmt = AV_PIX_FMT_YUVJ422P;
} else if ( monitor->OutputCodec() == "h264" ) {
video_out_codec = avcodec_find_encoder_by_name("h264_omx");
if ( ! video_out_codec ) {
Debug(1, "Didn't find omx");
video_out_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
}
video_out_ctx = avcodec_alloc_context3( video_out_codec );
}
// Copy params from instream to ctx
if ( video_in_stream && ( video_in_ctx->codec_id == AV_CODEC_ID_H264 ) ) {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
ret = avcodec_parameters_to_context(video_out_ctx, video_in_stream->codecpar);
#else
ret = avcodec_copy_context( video_out_ctx, video_in_ctx );
#endif
if ( ret < 0 ) {
Error("Could not initialize ctx parameteres");
return;
@ -130,9 +149,6 @@ VideoStore::VideoStore(
Debug(2, "Going to dump the outctx");
zm_dump_codec(video_out_ctx);
}
#else
avcodec_copy_context( video_out_ctx, video_in_ctx );
#endif
video_out_ctx->time_base = (AVRational){1, 1000000}; // microseconds as base frame rate
// Only set orientation if doing passthrough, otherwise the frame image will be rotated
Monitor::Orientation orientation = monitor->getOrientation();
@ -177,14 +193,6 @@ VideoStore::VideoStore(
video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
break;
}
// Same codec, just copy the packets, otherwise we have to decode/encode
video_out_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, NULL)) < 0 ) {
Warning("Can't open video codec (%s)! %s, trying h264",
video_out_codec->name,
av_make_error_string(ret).c_str()
);
}
} else {
@ -204,100 +212,77 @@ VideoStore::VideoStore(
#else
video_out_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
#endif
}
}
if ( monitor->OutputCodec() == "mjpeg" ) {
video_out_codec = avcodec_find_encoder_by_name("mjpeg");
if ( ! video_out_codec ) {
Debug(1, "Didn't find omx");
video_out_codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
}
video_out_ctx->codec_id = video_out_codec->id;
video_out_ctx->pix_fmt = AV_PIX_FMT_YUVJ422P;
} else if ( monitor->OutputCodec() == "h264" ) {
video_out_codec = avcodec_find_encoder_by_name("h264_omx");
if ( ! video_out_codec ) {
Debug(1, "Didn't find omx");
video_out_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
}
if ( !video_out_codec ) {
Fatal("Could not find codec for H264");
}
Debug(2, "Have video out codec");
video_out_ctx->codec_id = AV_CODEC_ID_H264;
//video_in_ctx->sample_aspect_ratio;
video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
/* video time_base can be set to whatever is handy and supported by encoder */
video_out_ctx->time_base = (AVRational){1, 1000000}; // microseconds as base frame rate
//video_out_ctx->framerate = (AVRational){0,24}; // Unknown framerate
#if 1
video_out_ctx->gop_size = 12;
video_out_ctx->qmin = 10;
video_out_ctx->qmax = 51;
video_out_ctx->qcompress = 0.6;
video_out_ctx->bit_rate = 4000000;
video_out_ctx->codec_id = AV_CODEC_ID_H264;
//video_in_ctx->sample_aspect_ratio;
video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
/* video time_base can be set to whatever is handy and supported by encoder */
video_out_ctx->time_base = (AVRational){1, 1000000}; // microseconds as base frame rate
//video_out_ctx->framerate = (AVRational){0,24}; // Unknown framerate
#if 0
video_out_ctx->gop_size = 12;
video_out_ctx->qmin = 10;
video_out_ctx->qmax = 51;
video_out_ctx->qcompress = 0.6;
video_out_ctx->bit_rate = 4000000;
#endif
video_out_ctx->max_b_frames = 1;
if (video_out_ctx->codec_id == AV_CODEC_ID_H264)
if ( video_out_ctx->codec_id == AV_CODEC_ID_H264 ) {
if ( video_out_ctx->priv_data ) {
Debug(2, "Setting preset to supoerfast");
av_opt_set(video_out_ctx->priv_data, "preset", "superfast", 0);
AVDictionary *opts = 0;
std::string Options = monitor->GetEncoderOptions();
ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 0);
if ( ret < 0 ) {
Warning("Could not parse ffmpeg encoder options list '%s'\n", Options.c_str());
Debug(2, "Setting preset to supoerfast");
} else {
AVDictionaryEntry *e = NULL;
while ( (e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL ) {
Debug( 3, "Encoder Option %s=%s", e->key, e->value );
}
Debug(2, "Not setting priv_data");
}
}
if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0 ) {
Warning("Can't open video codec (%s)! %s, trying h264",
video_out_codec->name,
av_make_error_string(ret).c_str()
);
video_out_codec = avcodec_find_encoder_by_name("h264");
AVDictionary *opts = 0;
std::string Options = monitor->GetEncoderOptions();
ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 0);
if ( ret < 0 ) {
Warning("Could not parse ffmpeg encoder options list '%s'\n", Options.c_str());
} else {
AVDictionaryEntry *e = NULL;
while ( (e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL ) {
Debug( 3, "Encoder Option %s=%s", e->key, e->value );
}
}
if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0 ) {
Warning("Can't open video codec (%s)! %s, trying h264",
video_out_codec->name,
av_make_error_string(ret).c_str()
);
video_out_codec = avcodec_find_encoder_by_name("h264");
if ( ! video_out_codec ) {
Error("Can't find h264 encoder");
video_out_codec = avcodec_find_encoder_by_name("libx264");
if ( ! video_out_codec ) {
Error("Can't find h264 encoder");
video_out_codec = avcodec_find_encoder_by_name("libx264");
if ( ! video_out_codec ) {
Error("Can't find libx264 encoder");
return;
}
}
if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0 ) {
Error("Can't open video codec (%s)! %s",
video_out_codec->name,
av_make_error_string(ret).c_str() );
Error("Can't find libx264 encoder");
return;
}
}
Debug(2,"Sucess opening codec");
AVDictionaryEntry *e = NULL;
while ( (e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL ) {
Warning( "Encoder Option %s not recognized by ffmpeg codec", e->key);
if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0 ) {
Error("Can't open video codec (%s)! %s",
video_out_codec->name,
av_make_error_string(ret).c_str() );
return;
}
av_dict_free(&opts);
}
Debug(2,"Sucess opening codec");
AVDictionaryEntry *e = NULL;
while ( (e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL ) {
Warning( "Encoder Option %s not recognized by ffmpeg codec", e->key);
}
av_dict_free(&opts);
if ( !video_out_ctx->codec_tag ) {
video_out_ctx->codec_tag =
av_codec_get_tag(oc->oformat->codec_tag, AV_CODEC_ID_H264 );
Debug(2, "No codec_tag, setting to h264 ? ");
}
} else {
Error("Codec not set");
} // end if codec == h264
swscale.SetDefaults(
video_in_ctx->pix_fmt,
video_out_ctx->pix_fmt,
video_out_ctx->width,
video_out_ctx->height
);
if ( !video_out_ctx->codec_tag ) {
video_out_ctx->codec_tag =
av_codec_get_tag(oc->oformat->codec_tag, AV_CODEC_ID_H264 );
Debug(2, "No codec_tag, setting to h264 ? ");
}
} // end if copying or trasncoding
video_out_stream = avformat_new_stream(oc, video_out_codec);
@ -892,7 +877,13 @@ void VideoStore::dumpPacket(AVPacket *pkt) {
snprintf(b, sizeof(b),
" pts: %" PRId64 ", dts: %" PRId64
", data: %p, size: %d, sindex: %d, dflags: %04x, s-pos: %" PRId64
", duration: %" PRId64 "\n",
", duration: %"
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
PRId64
#else
"d"
#endif
"\n",
pkt->pts,
pkt->dts,
pkt->data,
@ -962,6 +953,7 @@ int VideoStore::writeVideoFramePacket( ZMPacket * zm_packet ) {
out_frame->width = video_out_ctx->width;
out_frame->height = video_out_ctx->height;
out_frame->format = video_out_ctx->pix_fmt;
out_frame->duration = 0;
if ( ! zm_packet->in_frame ) {
Debug(2,"Have no in_frame");
@ -1045,6 +1037,7 @@ int VideoStore::writeVideoFramePacket( ZMPacket * zm_packet ) {
opkt.dts = opkt.pts;
if ( zm_packet->keyframe )
opkt.flags |= AV_PKT_FLAG_KEY;
opkt.duration = 0;
} else {
AVPacket *ipkt = &zm_packet->packet;