Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void AudioLibav::DecodeAudio(std::vector<char> *audio)
- {
- //Wasteful?
- avcodec_register_all();
- int vectorPosition = 0;
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int len, memlen;
- FILE *f, *outfile;
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- uint8_t memBuffer[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- AVPacket avpkt, memPkt;
- AVFrame *decoded_frame = NULL;
- av_init_packet(&avpkt);
- printf("Audio decoding\n");
- /* find the mpeg audio decoder */
- codec = avcodec_find_decoder(AV_CODEC_ID_PCM_S16LE);
- if (!codec) {
- fprintf(stderr, "codec not found\n");
- exit(1);
- }
- c = avcodec_alloc_context3(codec);
- //Bad assumption
- c->channels = 2;
- /* open it */
- if (avcodec_open2(c, codec, NULL) < 0) {
- fprintf(stderr, "could not open codec\n");
- exit(1);
- }
- f = fopen("test.wav", "rb");
- if (!f) {
- //fprintf(stderr, "could not open %s\n", filename);
- exit(1);
- }
- outfile = fopen("testraw", "wb");
- if (!outfile) {
- av_free(c);
- exit(1);
- }
- /* decode until eof */
- //avpkt.data = inbuf;
- //avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
- memPkt.data = memBuffer;
- if(audio->size() < AUDIO_INBUF_SIZE)
- {
- memPkt.size = audio->size();
- memcpy(memBuffer, &audio->at(0), audio->size());
- vectorPosition = audio->size();
- }
- else
- {
- memPkt.size = AUDIO_INBUF_SIZE;
- memcpy(memBuffer, &audio->at(0), AUDIO_INBUF_SIZE);
- vectorPosition += AUDIO_INBUF_SIZE;
- }
- std::cout << "AVPacket Size: " << avpkt.size;
- //while (avpkt.size > 0) {
- while (memPkt.size > 0) {
- std::cout << "Mempacket size: " << memPkt.size << '\n';
- int got_frame = 0;
- if (!decoded_frame) {
- if (!(decoded_frame = avcodec_alloc_frame())) {
- fprintf(stderr, "out of memory\n");
- exit(1);
- }
- } else
- avcodec_get_frame_defaults(decoded_frame);
- //len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
- memlen = avcodec_decode_audio4(c, decoded_frame, &got_frame, &memPkt);
- if (memlen < 0) {
- fprintf(stderr, "Error while decoding\n");
- exit(1);
- }
- if (got_frame) {
- /* if a frame has been decoded, output it */
- int data_size = av_samples_get_buffer_size(NULL, c->channels,
- decoded_frame->nb_samples,
- c->sample_fmt, 1);
- fwrite(decoded_frame->data[0], 1, data_size, outfile);
- }
- //avpkt.size -= len;
- //avpkt.data += len;
- memPkt.size -= memlen;
- memPkt.data += memlen;
- if(memPkt.size < AUDIO_REFILL_THRESH)
- {
- memmove(memBuffer, memPkt.data, memPkt.size);
- if((audio->size() - vectorPosition) < (AUDIO_INBUF_SIZE - memPkt.size) )
- {
- memcpy(memBuffer, &audio->at(vectorPosition), (audio->size() - vectorPosition));
- memlen = (audio->size() - vectorPosition);
- //vectorPosition = audio->size();
- }
- else
- {
- memcpy(memPkt.data + memPkt.size, &audio->at(vectorPosition), AUDIO_INBUF_SIZE - memPkt.size);
- vectorPosition += AUDIO_INBUF_SIZE - memPkt.size;
- memlen = AUDIO_INBUF_SIZE - memPkt.size;
- }
- if(memlen > 0)
- {
- memPkt.size += memlen;
- }
- }
- //if (avpkt.size < AUDIO_REFILL_THRESH) {
- /* Refill the input buffer, to avoid trying to decode
- * incomplete frames. Instead of this, one could also use
- * a parser, or use a proper container format through
- * libavformat. */
- // memmove(inbuf, avpkt.data, avpkt.size);
- // avpkt.data = inbuf;
- // len = fread(avpkt.data + avpkt.size, 1,
- // AUDIO_INBUF_SIZE - avpkt.size, f);
- // if (len > 0)
- // avpkt.size += len;
- //}
- }
- fclose(outfile);
- fclose(f);
- avcodec_close(c);
- av_free(c);
- avcodec_free_frame(&decoded_frame);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement