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 { } 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 */ /* 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); zm_packet.image->Assign(width, height, colours, subpixelorder, buffer, imagesize);

View File

@ -117,12 +117,31 @@ VideoStore::VideoStore(
video_in_stream_index = 0; 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 // Copy params from instream to ctx
if ( video_in_stream && ( video_in_ctx->codec_id == AV_CODEC_ID_H264 ) ) { if ( video_in_stream && ( video_in_ctx->codec_id == AV_CODEC_ID_H264 ) ) {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
ret = avcodec_parameters_to_context(video_out_ctx, video_in_stream->codecpar); 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 ) { if ( ret < 0 ) {
Error("Could not initialize ctx parameteres"); Error("Could not initialize ctx parameteres");
return; return;
@ -130,9 +149,6 @@ VideoStore::VideoStore(
Debug(2, "Going to dump the outctx"); Debug(2, "Going to dump the outctx");
zm_dump_codec(video_out_ctx); 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 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 // Only set orientation if doing passthrough, otherwise the frame image will be rotated
Monitor::Orientation orientation = monitor->getOrientation(); Monitor::Orientation orientation = monitor->getOrientation();
@ -177,14 +193,6 @@ VideoStore::VideoStore(
video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P; video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
break; 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 { } else {
@ -206,33 +214,13 @@ VideoStore::VideoStore(
#endif #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_out_ctx->codec_id = AV_CODEC_ID_H264;
//video_in_ctx->sample_aspect_ratio; //video_in_ctx->sample_aspect_ratio;
video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P; video_out_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
/* video time_base can be set to whatever is handy and supported by encoder */ /* 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->time_base = (AVRational){1, 1000000}; // microseconds as base frame rate
//video_out_ctx->framerate = (AVRational){0,24}; // Unknown framerate //video_out_ctx->framerate = (AVRational){0,24}; // Unknown framerate
#if 1 #if 0
video_out_ctx->gop_size = 12; video_out_ctx->gop_size = 12;
video_out_ctx->qmin = 10; video_out_ctx->qmin = 10;
video_out_ctx->qmax = 51; video_out_ctx->qmax = 51;
@ -240,8 +228,15 @@ VideoStore::VideoStore(
video_out_ctx->bit_rate = 4000000; video_out_ctx->bit_rate = 4000000;
#endif #endif
video_out_ctx->max_b_frames = 1; 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); av_opt_set(video_out_ctx->priv_data, "preset", "superfast", 0);
Debug(2, "Setting preset to supoerfast");
} else {
Debug(2, "Not setting priv_data");
}
}
AVDictionary *opts = 0; AVDictionary *opts = 0;
std::string Options = monitor->GetEncoderOptions(); std::string Options = monitor->GetEncoderOptions();
@ -288,16 +283,6 @@ Debug(2,"Sucess opening codec");
av_codec_get_tag(oc->oformat->codec_tag, AV_CODEC_ID_H264 ); av_codec_get_tag(oc->oformat->codec_tag, AV_CODEC_ID_H264 );
Debug(2, "No codec_tag, setting to 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
);
} // end if copying or trasncoding } // end if copying or trasncoding
video_out_stream = avformat_new_stream(oc, video_out_codec); video_out_stream = avformat_new_stream(oc, video_out_codec);
@ -892,7 +877,13 @@ void VideoStore::dumpPacket(AVPacket *pkt) {
snprintf(b, sizeof(b), snprintf(b, sizeof(b),
" pts: %" PRId64 ", dts: %" PRId64 " pts: %" PRId64 ", dts: %" PRId64
", data: %p, size: %d, sindex: %d, dflags: %04x, s-pos: %" 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->pts,
pkt->dts, pkt->dts,
pkt->data, pkt->data,
@ -962,6 +953,7 @@ int VideoStore::writeVideoFramePacket( ZMPacket * zm_packet ) {
out_frame->width = video_out_ctx->width; out_frame->width = video_out_ctx->width;
out_frame->height = video_out_ctx->height; out_frame->height = video_out_ctx->height;
out_frame->format = video_out_ctx->pix_fmt; out_frame->format = video_out_ctx->pix_fmt;
out_frame->duration = 0;
if ( ! zm_packet->in_frame ) { if ( ! zm_packet->in_frame ) {
Debug(2,"Have no in_frame"); Debug(2,"Have no in_frame");
@ -1045,6 +1037,7 @@ int VideoStore::writeVideoFramePacket( ZMPacket * zm_packet ) {
opkt.dts = opkt.pts; opkt.dts = opkt.pts;
if ( zm_packet->keyframe ) if ( zm_packet->keyframe )
opkt.flags |= AV_PKT_FLAG_KEY; opkt.flags |= AV_PKT_FLAG_KEY;
opkt.duration = 0;
} else { } else {
AVPacket *ipkt = &zm_packet->packet; AVPacket *ipkt = &zm_packet->packet;