Add a video output stream
| 1144 | |
| 1145 | // Add a video output stream |
| 1146 | AVStream *FFmpegWriter::add_video_stream() { |
| 1147 | // Find the video codec |
| 1148 | const AVCodec *codec = avcodec_find_encoder_by_name(info.vcodec.c_str()); |
| 1149 | if (codec == NULL) |
| 1150 | throw InvalidCodec("A valid video codec could not be found for this file.", path); |
| 1151 | |
| 1152 | // Free any previous memory allocations |
| 1153 | if (video_codec_ctx != nullptr) { |
| 1154 | AV_FREE_CONTEXT(video_codec_ctx); |
| 1155 | } |
| 1156 | |
| 1157 | // Create a new video stream |
| 1158 | AVStream* st = avformat_new_stream(oc, codec); |
| 1159 | if (!st) |
| 1160 | throw OutOfMemory("Could not allocate memory for the video stream.", path); |
| 1161 | |
| 1162 | // Allocate a new codec context for the stream |
| 1163 | ALLOC_CODEC_CTX(video_codec_ctx, codec, st) |
| 1164 | #if (LIBAVFORMAT_VERSION_MAJOR >= 58) |
| 1165 | st->codecpar->codec_id = codec->id; |
| 1166 | #endif |
| 1167 | |
| 1168 | AVCodecContext* c = video_codec_ctx; |
| 1169 | |
| 1170 | c->codec_id = codec->id; |
| 1171 | c->codec_type = AVMEDIA_TYPE_VIDEO; |
| 1172 | |
| 1173 | // Set sample aspect ratio |
| 1174 | c->sample_aspect_ratio.num = info.pixel_ratio.num; |
| 1175 | c->sample_aspect_ratio.den = info.pixel_ratio.den; |
| 1176 | |
| 1177 | /* Init video encoder options */ |
| 1178 | if (info.video_bit_rate >= 1000 |
| 1179 | #if (LIBAVCODEC_VERSION_MAJOR >= 58) |
| 1180 | && c->codec_id != AV_CODEC_ID_AV1 |
| 1181 | #endif |
| 1182 | ) { |
| 1183 | c->bit_rate = info.video_bit_rate; |
| 1184 | if (info.video_bit_rate >= 1500000) { |
| 1185 | if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
| 1186 | c->qmin = 2; |
| 1187 | c->qmax = 30; |
| 1188 | } |
| 1189 | } |
| 1190 | // Here should be the setting for low fixed bitrate |
| 1191 | // Defaults are used because mpeg2 otherwise had problems |
| 1192 | } else { |
| 1193 | // Check if codec supports crf or qp |
| 1194 | switch (c->codec_id) { |
| 1195 | #if (LIBAVCODEC_VERSION_MAJOR >= 58) |
| 1196 | // FFmpeg 4.0+ |
| 1197 | case AV_CODEC_ID_AV1 : |
| 1198 | // TODO: Set `crf` or `qp` according to bitrate, as bitrate is not supported by these encoders yet. |
| 1199 | if (info.video_bit_rate >= 1000) { |
| 1200 | c->bit_rate = 0; |
| 1201 | if (strstr(info.vcodec.c_str(), "aom") != NULL) { |
| 1202 | int calculated_quality = 35; |
| 1203 | if (info.video_bit_rate < 500000) calculated_quality = 50; |
nothing calls this directly
no test coverage detected