/* decode encoded audio; return the number of bytes decoded * negative indicates error */ int audio_decode_audio(struct iaxc_call * call, void * out, void * data, int len, int format, int * samples) { int insize = len; int outsize = *samples; timeLastOutput = iax_tvnow(); if ( format == 0 ) { fprintf(stderr, "audio_decode_audio: Format is zero (should't happen)!\n"); return -1; } /* destroy decoder if it is incorrect type */ if ( call->decoder && call->decoder->format != format ) { call->decoder->destroy(call->decoder); call->decoder = NULL; } /* create decoder if necessary */ if ( !call->decoder ) { call->decoder = create_codec(format); } if ( !call->decoder ) { fprintf(stderr, "ERROR: Codec could not be created: %d\n", format); return -1; } if ( call->decoder->decode(call->decoder, &insize, (unsigned char *)data, &outsize, (short *)out) ) { fprintf(stderr, "ERROR: decode error: %d\n", format); return -1; } output_postprocess(out, *samples - outsize); *samples = outsize; return len - insize; }
/* decode encoded audio; return the number of bytes decoded * negative indicates error */ int decode_audio(struct iaxc_call *call, void *out, void *data, int len, int format, int *samples) { int insize = len; int outsize = *samples; //fprintf(stderr, "in decode_audio, format=%d\n", format); /* update last output timestamp */ gettimeofday( &timeLastOutput, NULL ) ; //if(len == 0) fprintf(stderr, "Interpolation voice frame\n"); if(format == 0) { fprintf(stderr, "decode_audio: Format is zero (should't happen)!\n"); return -1; } /* destroy decoder if it is incorrect type */ if(call->decoder && call->decoder->format != format) { call->decoder->destroy(call->decoder); call->decoder = NULL; } /* create encoder if necessary */ if(!call->decoder) { call->decoder = create_codec(format); } if(!call->decoder) { /* ERROR: no codec */ fprintf(stderr, "ERROR: Codec could not be created: %d\n", format); return -1; } if(call->decoder->decode(call->decoder, &insize, data, &outsize, out)) { /* ERROR: codec error */ fprintf(stderr, "ERROR: decode error: %d\n", format); return -1; } output_postprocess(out, *samples-outsize); *samples = outsize; return len-insize; }
int send_encoded_audio(struct iaxc_call *call, void *data, int format, int samples) { unsigned char outbuf[1024]; int outsize = 1024; int silent; int insize = samples; //fprintf(stderr, "in encode_audio, format=%d\n", format); /* update last input timestamp */ gettimeofday( &timeLastInput, NULL ) ; silent = iaxc_input_postprocess(data,insize,8000); if(silent) { if(!call->tx_silent) { /* send a Comfort Noise Frame */ call->tx_silent = 1; if(iaxc_filters & IAXC_FILTER_CN) iax_send_cng(call->session, 10, NULL, 0); } return 0; /* poof! no encoding! */ } /* we're going to send voice now */ call->tx_silent = 0; /* destroy encoder if it is incorrect type */ if(call->encoder && call->encoder->format != format) { call->encoder->destroy(call->encoder); call->encoder = NULL; } /* just break early if there's no format defined: this happens for the * first couple of frames of new calls */ if(format == 0) return 0; /* create encoder if necessary */ if(!call->encoder) { call->encoder = create_codec(format); } if(!call->encoder) { /* ERROR: no codec */ fprintf(stderr, "ERROR: Codec could not be created: %d\n", format); return 0; } if(call->encoder->encode(call->encoder, &insize, (short *)data, &outsize, outbuf)) { /* ERROR: codec error */ fprintf(stderr, "ERROR: encode error: %d\n", format); return 0; } if(samples-insize == 0) { fprintf(stderr, "ERROR encoding (no samples output (samples=%d)\n", samples); return -1; } if(iax_send_voice(call->session,format, outbuf, 1024-outsize, samples-insize) == -1) { puts("Failed to send voice!"); return -1; } return 0; }
/* * compress_file * * Compresses the stream fin into foutname. This code is shamelessly * copied from libpcap's savefile.c:sf_next_packet() * * Inputs: * - fin: the uncompressed input stream * - lfout: the compressed output stream * - filehdr: the file header * - swapped: whether the packet order is swapped compared to the host order * * Output: * - return: 0 if ok, <0 if there was any problem * */ int compress_file (FILE *fin, lFILE *lfout, struct pcap_file_header *filehdr, int swapped) { codec_t *codec; int size; int hdr_size; u_char buffer[MAXDATABUFFER]; struct pcap_pkthdr pkthdr; int result; u_char *comp_pkt; int comp_len; u_int32_t bytes_written; co_t co; int paddings; /* get header size */ hdr_size = pktd_get_hdr_size (filehdr->linktype); /* create and initialize codec */ codec = create_codec(); /* get compression parameters */ co.rm_offset = DEFAULT_PKTD_COMPRESSION_RESTART_MARKER; co.ip_mask = DEFAULT_PKTD_COMPRESSION_IP_MASK; co.tcp_mask = DEFAULT_PKTD_COMPRESSION_TCP_MASK; co.udp_mask = DEFAULT_PKTD_COMPRESSION_UDP_MASK; /* write the new extended pcap header */ if (pktd_lfwrite_ext_header (lfout, 40, DLT_COMPRESSED, &co, NULL) < 0) { fprintf (stderr, "Error: cannot write header in file %s\n", foutname); return -1; } bytes_written = 0; while (1) { /* read the pcap header */ size = TCPDUMP_PACKET_HEADER_LENGTH; result = fread (&pkthdr, 1, size, fin); if (result != size) { if ((result == 0) && (feof (fin))) { break; } fprintf (stderr, "Error: fread'ing file %s\n", finname); return -1; } /* encode_trace requires pkthdr in host order */ if (swapped) { pktd_swap_pkthdr (&pkthdr); } /* The caplen and len fields were interchanged at version 2.3, * in order to match the bpf header layout. But unfortunately * some files were written with version 2.3 in their headers * but without the interchanged fields */ if (filehdr->version_minor < 3 || (filehdr->version_minor == 3 && pkthdr.caplen > pkthdr.len)) { int t = pkthdr.caplen; pkthdr.caplen = pkthdr.len; pkthdr.len = t; } /* read the packet itself */ size = pkthdr.caplen; result = fread (buffer, 1, size, fin); if (result != size) { if ((result == 0) && (feof (fin))) { break; } fprintf (stderr, "Error: freading file %s\n", finname); return -1; } /* encode_trace requires packet in network order, so no swap is needed */ /* encode the packet */ encode_trace (codec, &co, &pkthdr, buffer, pkthdr.caplen, hdr_size, &comp_pkt, &comp_len); /* introduce restart markers if needed */ if (co.rm_offset != 0) { paddings = need_restart_markers (&co, lfout, comp_len, bytes_written, codec); if (paddings < 0) { return -1; } else if (paddings > 0) { bytes_written += paddings; /* there was a codec initialization => must reencode packet */ encode_trace (codec, &co, &pkthdr, buffer, pkthdr.caplen, hdr_size, &comp_pkt, &comp_len); } } /* write compressed packet to file */ if (lfwrite (lfout, comp_pkt, comp_len) < comp_len) { return -1; } } return 0; }
/* * uncompress_file * * Uncompresses the stream fin into foutname * * Inputs: * - fin: the compressed input stream * - lfout: the uncompressed output stream * - co: compression parameters * * Output: * - return: 0 if ok, <0 if there was any problem * */ int uncompress_file (FILE *fin, lFILE *lfout, co_t *co) { codec_t *codec; int size; u_char compressed_buffer[MAX_COMPRESSED_LENGTH]; u_char buffer[MAXDATABUFFER]; struct pcap_pkthdr pkthdr; int result; /* create and initialize codec */ codec = create_codec(); /* write the new header */ if (pktd_write_header (lfileno(lfout), 40, DLT_RAW) < 0) { fprintf (stderr, "Error: cannot write header in file %s\n", foutname); return -1; } /* * every time you seek the rm_offset, you *must* initialize the codec (void)fseek (fin, co->rm_offset, SEEK_CUR); init_codec (codec); */ /* parse traces */ while (1) { /* read the trace length */ size = TCPDUMP_PACKET_HEADER_LENGTH_COMPRESSED; result = fread (compressed_buffer, 1, size, fin); if (result != size) { if ((result == 0) && (feof (fin))) { break; } fprintf (stderr, "Error: fread'ing file %s\n", finname); return -1; } size = (u_int8_t)(compressed_buffer[0]); /* look for escaped packets */ if (size == COMPRESSION_INIT_CODEC) { /* codec initialization requested */ init_codec (codec); continue; } else if (size == COMPRESSION_PADDING) { /* padding (empty) trace */ continue; } /* get the rest of the compressed packet */ result = fread (compressed_buffer+TCPDUMP_PACKET_HEADER_LENGTH_COMPRESSED, 1, size-TCPDUMP_PACKET_HEADER_LENGTH_COMPRESSED, fin); if (result != (size-TCPDUMP_PACKET_HEADER_LENGTH_COMPRESSED)) { if ((size == 0) && (feof (fin))) { break; } fprintf (stderr, "Error: freading file %s (%i)\n", finname, size); return -1; } /* decode the packet */ size = decode_trace (codec, compressed_buffer, &pkthdr, buffer); if (size < 0) { fprintf (stderr, "Error: decoding trace\n"); return -1; } /* write the uncompressed packet */ /* result = fwrite(buffer, 1, size, fout); */ result = lfwrite(lfout, buffer, size); if (result != size) { fprintf (stderr, "Error: fwriting file %s\n", foutname); return -1; } } return 0; }
int audio_send_encoded_audio(struct iaxc_call *call, int callNo, void *data, int format, int samples) { unsigned char outbuf[1024]; int outsize = 1024; int silent; int insize = samples; /* update last input timestamp */ timeLastInput = iax_tvnow(); silent = input_postprocess(data, insize, 8000); if(silent) { if(!call->tx_silent) { /* send a Comfort Noise Frame */ call->tx_silent = 1; if ( iaxci_filters & IAXC_FILTER_CN ) iax_send_cng(call->session, 10, NULL, 0); } return 0; /* poof! no encoding! */ } /* we're going to send voice now */ call->tx_silent = 0; /* destroy encoder if it is incorrect type */ if(call->encoder && call->encoder->format != format) { call->encoder->destroy(call->encoder); call->encoder = NULL; } /* just break early if there's no format defined: this happens for the * first couple of frames of new calls */ if(format == 0) return 0; /* create encoder if necessary */ if(!call->encoder) { call->encoder = create_codec(format); } if(!call->encoder) { /* ERROR: no codec */ fprintf(stderr, "ERROR: Codec could not be created: %d\n", format); return 0; } if(call->encoder->encode(call->encoder, &insize, (short *)data, &outsize, outbuf)) { /* ERROR: codec error */ fprintf(stderr, "ERROR: encode error: %d\n", format); return 0; } if(samples-insize == 0) { fprintf(stderr, "ERROR encoding (no samples output (samples=%d)\n", samples); return -1; } // Send the encoded audio data back to the app if required // TODO: fix the stupid way in which the encoded audio size is returned if ( iaxc_get_audio_prefs() & IAXC_AUDIO_PREF_RECV_LOCAL_ENCODED ) iaxci_do_audio_callback(callNo, 0, IAXC_SOURCE_LOCAL, 1, call->encoder->format & IAXC_AUDIO_FORMAT_MASK, sizeof(outbuf) - outsize, outbuf); if(iax_send_voice(call->session,format, outbuf, sizeof(outbuf) - outsize, samples-insize) == -1) { fprintf(stderr, "Failed to send voice! %s\n", iax_errstr); return -1; } return 0; }
struct VideoFile* video_file_open(const char* filename) { struct VideoFile* video_file = NULL; return_if(NULL == filename, NULL); video_file = (struct VideoFile*)malloc(sizeof(struct VideoFile)); if (video_file) { memset(video_file, 0, sizeof(struct VideoFile)); if (avformat_open_input(&(video_file->format_ctx), filename, NULL, NULL) == 0) { if (avformat_find_stream_info(video_file->format_ctx, NULL) >= 0) { int idx; /* find video stream */ idx = video_file->video_stream_idx = find_video_stream(video_file->format_ctx); if (idx >= 0) { video_file->video_stream = video_file->format_ctx->streams[idx]; video_file->codec_ctx = video_file->format_ctx->streams[idx]->codec; video_file->codec = create_codec(video_file->codec_ctx); if (video_file->codec) { /* * create software scaler context for colorspace * conversion */ video_file->scale_ctx = make_scale_context(video_file->codec_ctx); if (video_file->scale_ctx) { video_file->width = video_file->codec_ctx->width; video_file->height = video_file->codec_ctx->height; video_file->frame = avcodec_alloc_frame(); if (video_file->frame) { video_file->frame_rgb = avcodec_alloc_frame(); if (video_file->frame_rgb) { int bytes = avpicture_get_size( PIX_FMT_RGB24, video_file->width, video_file->height); video_file->rgb_buffer = (uint8_t *)av_malloc(bytes * sizeof(uint8_t)); if (video_file->rgb_buffer) { avpicture_fill( (AVPicture *)video_file->frame_rgb, video_file->rgb_buffer, PIX_FMT_RGB24, video_file->width, video_file->height); return video_file; } } } } } } } } video_file_close(video_file); video_file = NULL; } return video_file; }