SCM make_ffmpeg_input(SCM scm_file_name, SCM scm_debug) { SCM retval; struct ffmpeg_t *self; scm_dynwind_begin(0); const char *file_name = scm_to_locale_string(scm_file_name); scm_dynwind_free(file_name); self = (struct ffmpeg_t *)scm_gc_calloc(sizeof(struct ffmpeg_t), "ffmpeg"); self->video_stream_idx = -1; self->audio_stream_idx = -1; SCM_NEWSMOB(retval, ffmpeg_tag, self); int err; err = avformat_open_input(&self->fmt_ctx, file_name, NULL, NULL); if (err < 0) { ffmpeg_destroy(retval); scm_misc_error("make-ffmpeg-input", "Error opening file '~a': ~a", scm_list_2(scm_file_name, get_error_text(err))); }; err = avformat_find_stream_info(self->fmt_ctx, NULL); if (err < 0) { ffmpeg_destroy(retval); scm_misc_error("make-ffmpeg-input", "No stream information in file '~a': ~a", scm_list_2(scm_file_name, get_error_text(err))); }; // TODO: only open desired streams // Open video stream self->video_stream_idx = av_find_best_stream(self->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (self->video_stream_idx >= 0) self->video_codec_ctx = open_decoder(retval, scm_file_name, video_stream(self), "video"); // Open audio stream self->audio_stream_idx = av_find_best_stream(self->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if (self->audio_stream_idx >= 0) self->audio_codec_ctx = open_decoder(retval, scm_file_name, audio_stream(self), "audio"); // Print debug information if (scm_is_true(scm_debug)) av_dump_format(self->fmt_ctx, 0, file_name, 0); // Allocate input frames self->video_target_frame = allocate_frame(retval); self->audio_target_frame = allocate_frame(retval); // Initialise data packet av_init_packet(&self->pkt); self->pkt.data = NULL; self->pkt.size = 0; scm_dynwind_end(); return retval; }
/* ---------------------------------------------------------------------------- * * menu() * * ---------------------------------------------------------------------------- * * Presented at system startup. Allows the user to select between three * options by pressing certain keys on the keyboard: * 's' - Audio loopback streaming * 'n' - Tonal noise is generated by an NCO and added to the audio * being captured from the audio codec. * 'f' - The audio + tonal noise is passed to an adaptive LMS noise * cancellation filter which will use the tonal noise estimate * to remove the noise from the audio. * * This menu is shown upon exiting from any of the above options. * ---------------------------------------------------------------------------- */ void menu(){ u8 inp = 0x00; u32 CntrlRegister; /* Turn off all LEDs */ Xil_Out32(LED_BASE, 0); CntrlRegister = XUartPs_ReadReg(UART_BASEADDR, XUARTPS_CR_OFFSET); XUartPs_WriteReg(UART_BASEADDR, XUARTPS_CR_OFFSET, ((CntrlRegister & ~XUARTPS_CR_EN_DIS_MASK) | XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN)); xil_printf("\r\n\r\n"); xil_printf("Embedded LMS Filtering Demo\r\n"); xil_printf("Enter 's' to stream pure audio, 'n' to add tonal noise and 'f' to adaptively filter\r\n"); xil_printf("----------------------------------------\r\n"); // Wait for input from UART via the terminal while (!XUartPs_IsReceiveData(UART_BASEADDR)); inp = XUartPs_ReadReg(UART_BASEADDR, XUARTPS_FIFO_OFFSET); // Select function based on UART input switch(inp){ case 's': xil_printf("STREAMING AUDIO\r\n"); xil_printf("Press 'q' to return to the main menu\r\n"); audio_stream(); break; case 'n': xil_printf("ENTERING NOISE GENERATION OPERATION\r\n"); xil_printf("Select step size via the DIP switches...\r\n\n"); xil_printf("Press 'q' to return to the main menu\r\n"); tonal_noise(); break; case 'f': xil_printf("ENTERING LMS FILTERING OPERATION\r\n"); xil_printf("Press 'q' to return to the main menu\r\n"); lms_filter(); break; default: menu(); break; } // switch } // menu()
static int encode_audio(struct ffmpeg_t *self, AVFrame *audio_frame) { AVCodecContext *codec = audio_codec_ctx(self); // Initialise data packet AVPacket pkt = { 0 }; av_init_packet(&pkt); // Encode the audio frame int got_packet; int err = avcodec_encode_audio2(codec, &pkt, audio_frame, &got_packet); if (err < 0) scm_misc_error("encode-audio", "Error encoding audio frame: ~a", scm_list_1(get_error_text(err))); // Write any new audio packets if (got_packet) write_frame(self, &pkt, codec, audio_stream(self), self->audio_stream_idx); return got_packet; }
static SCM list_timestamped_audio(struct ffmpeg_t *self, AVFrame *frame) { return scm_list_2(scm_from_locale_symbol("audio"), scm_product(scm_from_int(frame_timestamp(frame)), time_base(audio_stream(self)))); }