no, you can't control how much audio is returned from av_read_frame. That function returns a "packet", which is a chunk of audio data, compressed. You pass data to avcodec_decode_audio2. The return value his how much data it managed to use in the decode; this is therefore in terms of compressed data, and for most audio types will equal packet.size (but not necessarily). The output buf_size is how much audio data was created from the compressed packet. How much data is in a packet is down to the stream, but all you really need to care about is that, on average, there is bitrate samples per second. How many packets that corresponds to does not matter.
Now I've no idea how also works, but at the end of the day, if your source file bitrate/sample width/number of channels matches alsa output, then if you're hardware is fast enough to decode on the fly, then you cannot have a problem. I suspect that when you say " Oddly enough, libav tells me that I'm reading in somewhere around 700 bytes" you're referring to the return value from avcodec_decode_audio2; you should be thinking in terms of buf_size instead. If you're still getting choppy audio then I guess you need to be buffering more before you send it to alsa. 2008/10/25 Dave Mulford <[EMAIL PROTECTED]> > That did help. I can now play sound that is somewhat familiar, however, now > it's very slow and choppy. Oddly enough, libav tells me that I'm reading in > somewhere around 700 bytes, whereas alsa tells me consistently that I'm > writing out 4608 bytes. So, I tried buffering the sound until I had around > 4600 bytes, but that doesn't work as sometimes I'll go over the 4608 bytes, > then try to write everything (sometimes somewhere around 5000), and I lose > data. > > Is there anyway to tell libav exactly how much data I want with each call > to > av_read_frame()? I've looked through the AVFormatContext and AVPacket > structures, but there doesn't seem to be a way to instruct libav how to do > so. > > Any ideas? > > Oh, I took out the buffering code as it was much more complex. Here is my > playsound() that now works...sort of. > > void > playsound() > { > AVPacket packet; > uint8_t *buf; > int lenread, lenwrite, buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; > > buf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE + > FF_INPUT_BUFFER_PADDING_SIZE); > > while (av_read_frame(formatContext, &packet) >= 0) > { > > buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; > > lenread = avcodec_decode_audio2(codecContext, buf, &buf_size, > packet.data, packet.size); > //fprintf(stderr, "playsound(): bytes read = %d\n", lenread); > > if (lenread <= 0) > { > fprintf(stderr, "playsound(): There was an error reading from > audio stream...\n"); > } > else > { > lenwrite = snd_pcm_writei(_soundDevice, buf, buf_size); > //fprintf(stderr, "playsound(): bytes written = %d\n", > lenwrite); > > if (lenwrite == -EPIPE) > { > fprintf(stderr, "playsound(): buffer underrun occurred\n"); > snd_pcm_prepare(_soundDevice); > } > } > } > } > > > On Sat, Oct 25, 2008 at 4:17 AM, slippyr4 <[EMAIL PROTECTED]> wrote: > > > avcodec_decode_audio2 takes a *pointer* to your output buffer size, > > specifically so it can modify the value. After the function returns, your > > buf_size variable will have the value of the number of bytes of output > > buffer actually used. Therefore, you need to reset it before each call. I > > suspect that the first audio packet was decoded successfully by your > > program, and it fell over thereafter. > > I'd do something like this:- > > > > int outbuf_used, outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; > > outbuf = avmalloc(outbuf_size); > > .... > > while (av_read_frame(formatContext, &packet) >= 0) > > { > > outbuf_used = outbuf_size; > > lenread = avcodec_decode_audio2(codecContext, buf, > > &outbuf_used, packet.data, packet.size); > > > > > > Also, I'm not sure why you're bothering to copy the packet.data to inbuf; > > you can just use the packet.data pointer in avcodec_decode_audio2. > > > > > > I hope this helps. > > > > 2008/10/25 Dave Mulford <[EMAIL PROTECTED]> > > > > > That didn't work, unfortunately. I still have the same issue :-( > > > > > > I had something similar before, which also didn't work. Now it just > seems > > > I'm trying things and hoping something works. Which is never the way to > > go. > > > > > > Anything else I can try? I'd really like to stick with libav* and alsa, > > but > > > if it's not the recommended way to do what I'm doing, then I'm all ears > > :-) > > > > > > Thanks, > > > mw007 > > > _______________________________________________ > > > libav-user mailing list > > > libav-user@mplayerhq.hu > > > https://lists.mplayerhq.hu/mailman/listinfo/libav-user > > > > > _______________________________________________ > > libav-user mailing list > > libav-user@mplayerhq.hu > > https://lists.mplayerhq.hu/mailman/listinfo/libav-user > > > _______________________________________________ > libav-user mailing list > libav-user@mplayerhq.hu > https://lists.mplayerhq.hu/mailman/listinfo/libav-user > _______________________________________________ libav-user mailing list libav-user@mplayerhq.hu https://lists.mplayerhq.hu/mailman/listinfo/libav-user