| 239 | } |
| 240 | |
| 241 | static void open_audio(AVFormatContext *oc, const AVCodec *codec, |
| 242 | OutputStream *ost, AVDictionary *opt_arg) |
| 243 | { |
| 244 | AVCodecContext *c; |
| 245 | int nb_samples; |
| 246 | int ret; |
| 247 | AVDictionary *opt = NULL; |
| 248 | |
| 249 | c = ost->enc; |
| 250 | |
| 251 | /* open it */ |
| 252 | av_dict_copy(&opt, opt_arg, 0); |
| 253 | ret = avcodec_open2(c, codec, &opt); |
| 254 | av_dict_free(&opt); |
| 255 | if (ret < 0) { |
| 256 | fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret)); |
| 257 | exit(1); |
| 258 | } |
| 259 | |
| 260 | /* init signal generator */ |
| 261 | ost->t = 0; |
| 262 | ost->tincr = 2 * M_PI * 110.0 / c->sample_rate; |
| 263 | /* increment frequency by 110 Hz per second */ |
| 264 | ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; |
| 265 | |
| 266 | if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) |
| 267 | nb_samples = 10000; |
| 268 | else |
| 269 | nb_samples = c->frame_size; |
| 270 | |
| 271 | ost->frame = alloc_audio_frame(c->sample_fmt, &c->ch_layout, |
| 272 | c->sample_rate, nb_samples); |
| 273 | ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, &c->ch_layout, |
| 274 | c->sample_rate, nb_samples); |
| 275 | |
| 276 | /* copy the stream parameters to the muxer */ |
| 277 | ret = avcodec_parameters_from_context(ost->st->codecpar, c); |
| 278 | if (ret < 0) { |
| 279 | fprintf(stderr, "Could not copy the stream parameters\n"); |
| 280 | exit(1); |
| 281 | } |
| 282 | |
| 283 | /* create resampler context */ |
| 284 | ost->swr_ctx = swr_alloc(); |
| 285 | if (!ost->swr_ctx) { |
| 286 | fprintf(stderr, "Could not allocate resampler context\n"); |
| 287 | exit(1); |
| 288 | } |
| 289 | |
| 290 | /* set options */ |
| 291 | av_opt_set_chlayout (ost->swr_ctx, "in_chlayout", &c->ch_layout, 0); |
| 292 | av_opt_set_int (ost->swr_ctx, "in_sample_rate", c->sample_rate, 0); |
| 293 | av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); |
| 294 | av_opt_set_chlayout (ost->swr_ctx, "out_chlayout", &c->ch_layout, 0); |
| 295 | av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0); |
| 296 | av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0); |
| 297 | |
| 298 | /* initialize the resampling context */ |
no test coverage detected