spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 6ea9814a56f0a2009917ccfbf3441522e62fa746
parent 4fc8811694d256d20c8a98a1c95d404082372c6c
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sat, 10 Jul 2010 13:01:53 +1000

More av_malloc fixes

Diffstat:
Msrc/spek-audio.c | 40++++++++++++++++++++++------------------
Msrc/spek-audio.h | 2+-
Msrc/spek-fft.c | 9+++++----
3 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/src/spek-audio.c b/src/spek-audio.c @@ -104,7 +104,8 @@ SpekAudioContext * spek_audio_open (const char *file_name) { } cx->buffer_size = (AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2; cx->buffer = av_malloc (cx->buffer_size); - av_init_packet (&cx->packet); + cx->packet = av_mallocz (sizeof (AVPacket)); + av_init_packet (cx->packet); cx->offset = 0; return cx; } @@ -127,17 +128,17 @@ gint spek_audio_read (SpekAudioContext *cx) { } for (;;) { - while (cx->packet.size > 0) { + while (cx->packet->size > 0) { buffer_size = cx->buffer_size; len = avcodec_decode_audio3 ( - cx->codec_context, (int16_t *) cx->buffer, &buffer_size, &cx->packet); + cx->codec_context, (int16_t *) cx->buffer, &buffer_size, cx->packet); if (len < 0) { /* Error, skip the frame. */ - cx->packet.size = 0; + cx->packet->size = 0; break; } - cx->packet.data += len; - cx->packet.size -= len; + cx->packet->data += len; + cx->packet->size -= len; cx->offset += len; if (buffer_size <= 0) { /* No data yet, get more frames */ @@ -146,17 +147,17 @@ gint spek_audio_read (SpekAudioContext *cx) { /* We have data, return it and come back for more later */ return buffer_size; } - if (cx->packet.data) { - cx->packet.data -= cx->offset; - cx->packet.size += cx->offset; + if (cx->packet->data) { + cx->packet->data -= cx->offset; + cx->packet->size += cx->offset; cx->offset = 0; - av_free_packet (&cx->packet); + av_free_packet (cx->packet); } - while ((res = av_read_frame (cx->format_context, &cx->packet)) >= 0) { - if (cx->packet.stream_index == cx->audio_stream) { + while ((res = av_read_frame (cx->format_context, cx->packet)) >= 0) { + if (cx->packet->stream_index == cx->audio_stream) { break; } - av_free_packet (&cx->packet); + av_free_packet (cx->packet); } if (res < 0) { /* End of file or error. */ @@ -175,11 +176,14 @@ void spek_audio_close (SpekAudioContext *cx) { if (cx->buffer) { av_free (cx->buffer); } - if (cx->packet.data) { - cx->packet.data -= cx->offset; - cx->packet.size += cx->offset; - cx->offset = 0; - av_free_packet (&cx->packet); + if (cx->packet) { + if (cx->packet->data) { + cx->packet->data -= cx->offset; + cx->packet->size += cx->offset; + cx->offset = 0; + av_free_packet (cx->packet); + } + av_free (cx->packet); } if (cx->codec_context != NULL) { avcodec_close (cx->codec_context); diff --git a/src/spek-audio.h b/src/spek-audio.h @@ -31,7 +31,7 @@ typedef struct { AVStream *stream; AVCodec *codec; gint buffer_size; - AVPacket packet; + AVPacket *packet; gint offset; /* Exposed properties */ diff --git a/src/spek-fft.c b/src/spek-fft.c @@ -17,14 +17,15 @@ */ #include <math.h> +#include <libavutil/mem.h> #include "spek-fft.h" SpekFftPlan * spek_fft_plan_new (gint n, gint threshold) { gint bits; SpekFftPlan *p = g_new0 (SpekFftPlan, 1); - p->input = g_new0 (gfloat, n); - p->output = g_new0 (gfloat, n / 2 + 1); + p->input = av_mallocz (sizeof (gfloat) * n); + p->output = av_mallocz (sizeof (gfloat) * (n / 2 + 1)); p->threshold = threshold; for (bits = 0; n; n >>= 1, bits++); p->n = 1 << --bits; @@ -52,7 +53,7 @@ void spek_fft_execute (SpekFftPlan *p) { void spek_fft_destroy (SpekFftPlan *p) { av_rdft_end (p->cx); - g_free (p->input); - g_free (p->output); + av_free (p->input); + av_free (p->output); g_free (p); }