open audio codec
| 1338 | |
| 1339 | // open audio codec |
| 1340 | void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) { |
| 1341 | const AVCodec *codec; |
| 1342 | AV_GET_CODEC_FROM_STREAM(st, audio_codec_ctx) |
| 1343 | |
| 1344 | // Audio encoding does not typically use more than 2 threads (most codecs use 1 thread) |
| 1345 | audio_codec_ctx->thread_count = std::min(FF_AUDIO_NUM_PROCESSORS, 2); |
| 1346 | |
| 1347 | // Find the audio encoder |
| 1348 | codec = avcodec_find_encoder_by_name(info.acodec.c_str()); |
| 1349 | if (!codec) |
| 1350 | codec = avcodec_find_encoder(audio_codec_ctx->codec_id); |
| 1351 | if (!codec) |
| 1352 | throw InvalidCodec("Could not find codec", path); |
| 1353 | |
| 1354 | // Init options |
| 1355 | AVDictionary *opts = NULL; |
| 1356 | av_dict_set(&opts, "strict", "experimental", 0); |
| 1357 | |
| 1358 | // Open the codec |
| 1359 | if (avcodec_open2(audio_codec_ctx, codec, &opts) < 0) |
| 1360 | throw InvalidCodec("Could not open audio codec", path); |
| 1361 | AV_COPY_PARAMS_FROM_CONTEXT(st, audio_codec_ctx); |
| 1362 | |
| 1363 | // Free options |
| 1364 | av_dict_free(&opts); |
| 1365 | |
| 1366 | // Calculate the size of the input frame (i..e how many samples per packet), and the output buffer |
| 1367 | // TODO: Ugly hack for PCM codecs (will be removed ASAP with new PCM support to compute the input frame size in samples |
| 1368 | if (audio_codec_ctx->frame_size <= 1) { |
| 1369 | // No frame size found... so calculate |
| 1370 | audio_input_frame_size = 50000 / info.channels; |
| 1371 | |
| 1372 | int s = AV_FIND_DECODER_CODEC_ID(st); |
| 1373 | switch (s) { |
| 1374 | case AV_CODEC_ID_PCM_S16LE: |
| 1375 | case AV_CODEC_ID_PCM_S16BE: |
| 1376 | case AV_CODEC_ID_PCM_U16LE: |
| 1377 | case AV_CODEC_ID_PCM_U16BE: |
| 1378 | audio_input_frame_size >>= 1; |
| 1379 | break; |
| 1380 | default: |
| 1381 | break; |
| 1382 | } |
| 1383 | } else { |
| 1384 | // Set frame size based on the codec |
| 1385 | audio_input_frame_size = audio_codec_ctx->frame_size; |
| 1386 | } |
| 1387 | |
| 1388 | // Set the initial frame size (since it might change during resampling) |
| 1389 | initial_audio_input_frame_size = audio_input_frame_size; |
| 1390 | |
| 1391 | // Allocate array for samples |
| 1392 | samples = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE]; |
| 1393 | |
| 1394 | // Set audio output buffer (used to store the encoded audio) |
| 1395 | audio_outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
| 1396 | audio_outbuf = new uint8_t[audio_outbuf_size]; |
| 1397 |
nothing calls this directly
no test coverage detected