[nut]: r142 - in trunk/libnut: demuxer.c muxer.c nut.h priv.h reorder.c

Author: ods15 Date: Sat Sep 23 20:34:54 2006 New Revision: 142 Modified: trunk/libnut/demuxer.c trunk/libnut/muxer.c trunk/libnut/nut.h trunk/libnut/priv.h trunk/libnut/reorder.c Log: big change to user malloc Modified: trunk/libnut/demuxer.c ============================================================================== --- trunk/libnut/demuxer.c (original) +++ trunk/libnut/demuxer.c Sat Sep 23 20:34:54 2006 @@ -30,7 +30,7 @@ amount += 10; // ### + PREALLOC_SIZE ? if (bc->write_len - pos < amount) { bc->write_len = amount + pos + PREALLOC_SIZE; - bc->buf = realloc(bc->buf, bc->write_len); + bc->buf = bc->alloc->realloc(bc->buf, bc->write_len); bc->buf_ptr = bc->buf + pos; } bc->read_len += bc->isc.read(bc->isc.priv, amount - (bc->read_len - pos), bc->buf + bc->read_len); @@ -87,11 +87,13 @@ bc->file_pos = 0; bc->filesize = 0; bc->buf_ptr = bc->buf = NULL; + bc->alloc = NULL; return bc; } -static input_buffer_t * new_input_buffer(nut_input_stream_t isc) { - input_buffer_t * bc = new_mem_buffer(malloc(sizeof(input_buffer_t))); +static input_buffer_t * new_input_buffer(nut_alloc_t * alloc, nut_input_stream_t isc) { + input_buffer_t * bc = new_mem_buffer(alloc->malloc(sizeof(input_buffer_t))); + bc->alloc = alloc; bc->is_mem = 0; bc->isc = isc; bc->file_pos = isc.file_pos; @@ -104,10 +106,10 @@ } static void free_buffer(input_buffer_t * bc) { - assert(!bc->is_mem); if (!bc) return; - free(bc->buf); - free(bc); + assert(!bc->is_mem); + bc->alloc->free(bc->buf); + bc->alloc->free(bc); } static int get_bytes(input_buffer_t * bc, int count, uint64_t * val) { @@ -194,12 +196,12 @@ return len; } -static int get_vb(input_buffer_t * in, int * len, uint8_t ** buf) { +static int get_vb(nut_alloc_t * alloc, input_buffer_t * in, int * len, uint8_t ** buf) { uint64_t tmp; int err; if ((err = get_v(in, &tmp))) return err; *len = tmp; - *buf = realloc(*buf, tmp); + *buf = alloc->realloc(*buf, tmp); if (get_data(in, tmp, *buf) != tmp) return buf_eof(in); return 0; } @@ -242,7 +244,7 @@ if (nut->max_distance > 65536) nut->max_distance = 65536; GET_V(tmp, nut->timebase_count); - nut->tb = realloc(nut->tb, nut->timebase_count * sizeof(nut_timebase_t)); + nut->tb = nut->alloc->realloc(nut->tb, nut->timebase_count * sizeof(nut_timebase_t)); for (i = 0; i < nut->timebase_count; i++) { GET_V(tmp, nut->tb[i].nom); GET_V(tmp, nut->tb[i].den); @@ -293,7 +295,7 @@ ERROR(i != id, -ERR_BAD_STREAM_ORDER); GET_V(tmp, sc->sh.type); - CHECK(get_vb(tmp, &sc->sh.fourcc_len, &sc->sh.fourcc)); + CHECK(get_vb(nut->alloc, tmp, &sc->sh.fourcc_len, &sc->sh.fourcc)); GET_V(tmp, sc->timebase_id); sc->sh.time_base = nut->tb[sc->timebase_id]; GET_V(tmp, sc->msb_pts_shift); @@ -301,7 +303,7 @@ GET_V(tmp, sc->sh.decode_delay); GET_V(tmp, i); // stream_flags sc->sh.fixed_fps = i & 1; - CHECK(get_vb(tmp, &sc->sh.codec_specific_len, &sc->sh.codec_specific)); + CHECK(get_vb(nut->alloc, tmp, &sc->sh.codec_specific_len, &sc->sh.codec_specific)); switch (sc->sh.type) { case NUT_VIDEO_CLASS: @@ -345,9 +347,9 @@ i++; if (sl->len + 1 > sl->alloc_len) { sl->alloc_len += PREALLOC_SIZE/4; - sl->s = realloc(sl->s, sl->alloc_len * sizeof(syncpoint_t)); - sl->pts = realloc(sl->pts, sl->alloc_len * nut->stream_count * sizeof(uint64_t)); - sl->eor = realloc(sl->eor, sl->alloc_len * nut->stream_count * sizeof(uint64_t)); + sl->s = nut->alloc->realloc(sl->s, sl->alloc_len * sizeof(syncpoint_t)); + sl->pts = nut->alloc->realloc(sl->pts, sl->alloc_len * nut->stream_count * sizeof(uint64_t)); + sl->eor = nut->alloc->realloc(sl->eor, sl->alloc_len * nut->stream_count * sizeof(uint64_t)); } memmove(sl->s + i + 1, sl->s + i, (sl->len - i) * sizeof(syncpoint_t)); memmove(sl->pts + (i + 1) * nut->stream_count, sl->pts + i * nut->stream_count, (sl->len - i) * nut->stream_count * sizeof(uint64_t)); @@ -436,9 +438,9 @@ GET_V(tmp, x); sl->alloc_len = sl->len = x; - sl->s = realloc(sl->s, sl->alloc_len * sizeof(syncpoint_t)); - sl->pts = realloc(sl->pts, sl->alloc_len * sizeof(uint64_t) * nut->stream_count); - sl->eor = realloc(sl->eor, sl->alloc_len * sizeof(uint64_t) * nut->stream_count); + sl->s = nut->alloc->realloc(sl->s, sl->alloc_len * sizeof(syncpoint_t)); + sl->pts = nut->alloc->realloc(sl->pts, sl->alloc_len * sizeof(uint64_t) * nut->stream_count); + sl->eor = nut->alloc->realloc(sl->eor, sl->alloc_len * sizeof(uint64_t) * nut->stream_count); for (i = 0; i < sl->len; i++) { GET_V(tmp, sl->s[i].pos); @@ -718,7 +720,7 @@ CHECK(get_main_header(nut)); if (!nut->sc) { - nut->sc = malloc(sizeof(stream_context_t) * nut->stream_count); + nut->sc = nut->alloc->malloc(sizeof(stream_context_t) * nut->stream_count); for (i = 0; i < nut->stream_count; i++) { nut->sc[i].last_pts = 0; nut->sc[i].last_dts = 0; @@ -742,7 +744,7 @@ } CHECK(get_stream_header(nut, i)); if (!nut->sc[i].pts_cache) { - nut->sc[i].pts_cache = malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); + nut->sc[i].pts_cache = nut->alloc->malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); for (j = 0; j < nut->sc[i].sh.decode_delay; j++) nut->sc[i].pts_cache[j] = -1; } @@ -784,17 +786,17 @@ if (nut->before_seek) seek_buf(nut->i, nut->before_seek, SEEK_SET); nut->before_seek = 0; } - *s = malloc(sizeof(nut_stream_header_t) * (nut->stream_count + 1)); + *s = nut->alloc->malloc(sizeof(nut_stream_header_t) * (nut->stream_count + 1)); for (i = 0; i < nut->stream_count; i++) (*s)[i] = nut->sc[i].sh; (*s)[i].type = -1; err_out: if (err && err != 2 && !nut->seek_status) { if (nut->sc) for (i = 0; i < nut->stream_count; i++) { - free(nut->sc[i].sh.fourcc); - free(nut->sc[i].sh.codec_specific); - free(nut->sc[i].pts_cache); + nut->alloc->free(nut->sc[i].sh.fourcc); + nut->alloc->free(nut->sc[i].sh.codec_specific); + nut->alloc->free(nut->sc[i].pts_cache); } - free(nut->sc); + nut->alloc->free(nut->sc); nut->sc = NULL; nut->stream_count = 0; } @@ -1225,19 +1227,20 @@ if (err != 2) { // unless EAGAIN flush_buf(nut->i); nut->before_seek = 0; - free(nut->seek_state); + nut->alloc->free(nut->seek_state); nut->seek_state = NULL; } else { - if (!nut->seek_state) nut->seek_state = malloc(sizeof state); + if (!nut->seek_state) nut->seek_state = nut->alloc->malloc(sizeof state); memcpy(nut->seek_state, state, sizeof state); } return err; } nut_context_t * nut_demuxer_init(nut_demuxer_opts_t * dopts) { - nut_context_t * nut = malloc(sizeof(nut_context_t)); + nut_context_t * nut; - nut->i = new_input_buffer(dopts->input); + if (dopts->alloc.malloc) nut = dopts->alloc.malloc(sizeof(nut_context_t)); + else nut = malloc(sizeof(nut_context_t)); nut->syncpoints.len = 0; nut->syncpoints.alloc_len = 0; @@ -1254,6 +1257,17 @@ nut->before_seek = 0; nut->last_syncpoint = 0; nut->seek_state = NULL; + + nut->alloc = &nut->dopts.alloc; + + if (!nut->alloc->malloc) { + nut->alloc->malloc = malloc; + nut->alloc->realloc = realloc; + nut->alloc->free = free; + } + + nut->i = new_input_buffer(nut->alloc, dopts->input); + return nut; } @@ -1261,19 +1275,19 @@ int i; if (!nut) return; for (i = 0; i < nut->stream_count; i++) { - free(nut->sc[i].sh.fourcc); - free(nut->sc[i].sh.codec_specific); - free(nut->sc[i].pts_cache); + nut->alloc->free(nut->sc[i].sh.fourcc); + nut->alloc->free(nut->sc[i].sh.codec_specific); + nut->alloc->free(nut->sc[i].pts_cache); } - free(nut->syncpoints.s); - free(nut->syncpoints.pts); - free(nut->syncpoints.eor); - free(nut->sc); - free(nut->tb); - free(nut->seek_state); + nut->alloc->free(nut->syncpoints.s); + nut->alloc->free(nut->syncpoints.pts); + nut->alloc->free(nut->syncpoints.eor); + nut->alloc->free(nut->sc); + nut->alloc->free(nut->tb); + nut->alloc->free(nut->seek_state); free_buffer(nut->i); - free(nut); + nut->alloc->free(nut); } const char * nut_error(int error) { Modified: trunk/libnut/muxer.c ============================================================================== --- trunk/libnut/muxer.c (original) +++ trunk/libnut/muxer.c Sat Sep 23 20:34:54 2006 @@ -23,29 +23,30 @@ if (bc->is_mem) { int tmp = bc->buf_ptr - bc->buf; bc->write_len = tmp + amount + PREALLOC_SIZE; - bc->buf = realloc(bc->buf, bc->write_len); + bc->buf = bc->alloc->realloc(bc->buf, bc->write_len); bc->buf_ptr = bc->buf + tmp; } else { flush_buf(bc); if (bc->write_len < amount) { - free(bc->buf); + bc->alloc->free(bc->buf); bc->write_len = amount + PREALLOC_SIZE; - bc->buf_ptr = bc->buf = malloc(bc->write_len); + bc->buf_ptr = bc->buf = bc->alloc->malloc(bc->write_len); } } } -static output_buffer_t * new_mem_buffer() { - output_buffer_t * bc = malloc(sizeof(output_buffer_t)); +static output_buffer_t * new_mem_buffer(nut_alloc_t * alloc) { + output_buffer_t * bc = alloc->malloc(sizeof(output_buffer_t)); + bc->alloc = alloc; bc->write_len = PREALLOC_SIZE; bc->is_mem = 1; bc->file_pos = 0; - bc->buf_ptr = bc->buf = malloc(bc->write_len); + bc->buf_ptr = bc->buf = alloc->malloc(bc->write_len); return bc; } -static output_buffer_t * new_output_buffer(nut_output_stream_t osc) { - output_buffer_t * bc = new_mem_buffer(); +static output_buffer_t * new_output_buffer(nut_alloc_t * alloc, nut_output_stream_t osc) { + output_buffer_t * bc = new_mem_buffer(alloc); bc->is_mem = 0; bc->osc = osc; if (!bc->osc.write) bc->osc.write = stream_write; @@ -55,8 +56,8 @@ static void free_buffer(output_buffer_t * bc) { if (!bc) return; if (!bc->is_mem) flush_buf(bc); - free(bc->buf); - free(bc); + bc->alloc->free(bc->buf); + bc->alloc->free(bc); } static output_buffer_t * clear_buffer(output_buffer_t * bc) { @@ -282,9 +283,9 @@ if (s->alloc_len <= s->len) { s->alloc_len += PREALLOC_SIZE; - s->s = realloc(s->s, s->alloc_len * sizeof(syncpoint_t)); - s->pts = realloc(s->pts, s->alloc_len * nut->stream_count * sizeof(uint64_t)); - s->eor = realloc(s->eor, s->alloc_len * nut->stream_count * sizeof(uint64_t)); + s->s = nut->alloc->realloc(s->s, s->alloc_len * sizeof(syncpoint_t)); + s->pts = nut->alloc->realloc(s->pts, s->alloc_len * nut->stream_count * sizeof(uint64_t)); + s->eor = nut->alloc->realloc(s->eor, s->alloc_len * nut->stream_count * sizeof(uint64_t)); } for (i = 0; i < nut->stream_count; i++) { @@ -487,16 +488,28 @@ } nut_context_t * nut_muxer_init(const nut_muxer_opts_t * mopts, const nut_stream_header_t s[], const nut_info_packet_t info[]) { - nut_context_t * nut = malloc(sizeof(nut_context_t)); + nut_context_t * nut; int i; // TODO check that all input is valid - nut->o = new_output_buffer(mopts->output); - nut->tmp_buffer = new_mem_buffer(); // general purpose buffer - nut->tmp_buffer2 = new_mem_buffer(); // for packet_headers - nut->max_distance = mopts->max_distance; + if (mopts->alloc.malloc) nut = mopts->alloc.malloc(sizeof(nut_context_t)); + else nut = malloc(sizeof(nut_context_t)); + nut->mopts = *mopts; + nut->alloc = &nut->mopts.alloc; + + if (!nut->alloc->malloc) { + nut->alloc->malloc = malloc; + nut->alloc->realloc = realloc; + nut->alloc->free = free; + } + + nut->o = new_output_buffer(nut->alloc, mopts->output); + nut->tmp_buffer = new_mem_buffer(nut->alloc); // general purpose buffer + nut->tmp_buffer2 = new_mem_buffer(nut->alloc); // for packet_headers + nut->max_distance = mopts->max_distance; + if (nut->max_distance > 65536) nut->max_distance = 65536; { @@ -542,8 +555,8 @@ for (nut->stream_count = 0; s[nut->stream_count].type >= 0; nut->stream_count++); - nut->sc = malloc(sizeof(stream_context_t) * nut->stream_count); - nut->tb = malloc(sizeof(nut_timebase_t) * nut->stream_count); + nut->sc = nut->alloc->malloc(sizeof(stream_context_t) * nut->stream_count); + nut->tb = nut->alloc->malloc(sizeof(nut_timebase_t) * nut->stream_count); nut->timebase_count = 0; for (i = 0; i < nut->stream_count; i++) { @@ -557,20 +570,20 @@ nut->sc[i].sh = s[i]; nut->sc[i].sh.max_pts = 0; - nut->sc[i].sh.fourcc = malloc(s[i].fourcc_len); + nut->sc[i].sh.fourcc = nut->alloc->malloc(s[i].fourcc_len); memcpy(nut->sc[i].sh.fourcc, s[i].fourcc, s[i].fourcc_len); - nut->sc[i].sh.codec_specific = malloc(s[i].codec_specific_len); + nut->sc[i].sh.codec_specific = nut->alloc->malloc(s[i].codec_specific_len); memcpy(nut->sc[i].sh.codec_specific, s[i].codec_specific, s[i].codec_specific_len); - nut->sc[i].pts_cache = malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); + nut->sc[i].pts_cache = nut->alloc->malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); for (j = 0; j < nut->timebase_count; j++) if (compare_ts(nut, 1, s[i].time_base, 1, nut->tb[j]) == 0) break; if (j == nut->timebase_count) nut->tb[nut->timebase_count++] = s[i].time_base; nut->sc[i].timebase_id = j; // reorder.c - nut->sc[i].reorder_pts_cache = malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); + nut->sc[i].reorder_pts_cache = nut->alloc->malloc(nut->sc[i].sh.decode_delay * sizeof(int64_t)); for (j = 0; j < nut->sc[i].sh.decode_delay; j++) nut->sc[i].reorder_pts_cache[j] = nut->sc[i].pts_cache[j] = -1; nut->sc[i].next_pts = 0; nut->sc[i].packets = NULL; @@ -585,16 +598,16 @@ if (info) { for (nut->info_count = 0; info[nut->info_count].count >= 0; nut->info_count++); - nut->info = malloc(sizeof(nut_info_packet_t) * nut->info_count); + nut->info = nut->alloc->malloc(sizeof(nut_info_packet_t) * nut->info_count); for (i = 0; i < nut->info_count; i++) { int j; nut->info[i] = info[i]; - nut->info[i].fields = malloc(sizeof(nut_info_field_t) * info[i].count); + nut->info[i].fields = nut->alloc->malloc(sizeof(nut_info_field_t) * info[i].count); for (j = 0; j < info[i].count; j++) { nut->info[i].fields[j] = info[i].fields[j]; if (info[i].fields[j].data) { - nut->info[i].fields[j].data = malloc(info[i].fields[j].val); + nut->info[i].fields[j].data = nut->alloc->malloc(info[i].fields[j].val); memcpy(nut->info[i].fields[j].data, info[i].fields[j].data, info[i].fields[j].val); } } @@ -631,31 +644,31 @@ fprintf(stderr, "packet size: %.2lf ", (double)nut->sc[i].tot_size / nut->sc[i].total_frames); fprintf(stderr, "packet overhead: %.2lf\n", (double)nut->sc[i].overhead / nut->sc[i].total_frames); - free(nut->sc[i].sh.fourcc); - free(nut->sc[i].sh.codec_specific); - free(nut->sc[i].pts_cache); - free(nut->sc[i].reorder_pts_cache); + nut->alloc->free(nut->sc[i].sh.fourcc); + nut->alloc->free(nut->sc[i].sh.codec_specific); + nut->alloc->free(nut->sc[i].pts_cache); + nut->alloc->free(nut->sc[i].reorder_pts_cache); } - free(nut->sc); - free(nut->tb); + nut->alloc->free(nut->sc); + nut->alloc->free(nut->tb); for (i = 0; i < nut->info_count; i++) { int j; - for (j = 0; j < nut->info[i].count; j++) free(nut->info[i].fields[j].data); - free(nut->info[i].fields); + for (j = 0; j < nut->info[i].count; j++) nut->alloc->free(nut->info[i].fields[j].data); + nut->alloc->free(nut->info[i].fields); } - free(nut->info); + nut->alloc->free(nut->info); fprintf(stderr, "Syncpoints: %d size: %d\n", nut->syncpoints.len, nut->sync_overhead); - free(nut->syncpoints.s); - free(nut->syncpoints.pts); - free(nut->syncpoints.eor); + nut->alloc->free(nut->syncpoints.s); + nut->alloc->free(nut->syncpoints.pts); + nut->alloc->free(nut->syncpoints.eor); free_buffer(nut->tmp_buffer); free_buffer(nut->tmp_buffer2); fprintf(stderr, "TOTAL: %d bytes data, %d bytes overhead, %.2lf%% overhead\n", total, (int)bctello(nut->o) - total, (double)(bctello(nut->o) - total) / total*100); free_buffer(nut->o); // flushes file - free(nut); + nut->alloc->free(nut); } Modified: trunk/libnut/nut.h ============================================================================== --- trunk/libnut/nut.h (original) +++ trunk/libnut/nut.h Sat Sep 23 20:34:54 2006 @@ -66,7 +66,14 @@ } frame_table_input_t; typedef struct { + void * (*malloc)(size_t size); + void * (*realloc)(void *ptr, size_t size); + void (*free)(void *ptr); +} nut_alloc_t; + +typedef struct { nut_output_stream_t output; + nut_alloc_t alloc; int write_index; int max_distance; frame_table_input_t * fti; @@ -74,6 +81,7 @@ typedef struct { nut_input_stream_t input; + nut_alloc_t alloc; int read_index; //int cache_syncpoints; } nut_demuxer_opts_t; Modified: trunk/libnut/priv.h ============================================================================== --- trunk/libnut/priv.h (original) +++ trunk/libnut/priv.h Sat Sep 23 20:34:54 2006 @@ -62,6 +62,7 @@ int read_len; // data in memory off_t file_pos; off_t filesize; + nut_alloc_t * alloc; } input_buffer_t; typedef struct { @@ -71,6 +72,7 @@ uint8_t * buf_ptr; int write_len; // memory allocated off_t file_pos; + nut_alloc_t * alloc; } output_buffer_t; typedef struct { @@ -136,6 +138,7 @@ struct nut_context_s { nut_muxer_opts_t mopts; nut_demuxer_opts_t dopts; + nut_alloc_t * alloc; input_buffer_t * i; output_buffer_t * o; output_buffer_t * tmp_buffer; Modified: trunk/libnut/reorder.c ============================================================================== --- trunk/libnut/reorder.c (original) +++ trunk/libnut/reorder.c Sat Sep 23 20:34:54 2006 @@ -12,13 +12,13 @@ assert(amount <= s->num_packets); for (i = 0; i < amount; i++) { nut_write_frame(nut, &s->packets[i].p, s->packets[i].buf); - free(s->packets[i].buf); // FIXME + nut->alloc->free(s->packets[i].buf); // FIXME } if (s->next_pts != -2) s->next_pts = s->packets[i - 1].p.next_pts; s->num_packets -= amount; memmove(s->packets, s->packets + amount, s->num_packets * sizeof(reorder_packet_t)); - s->packets = realloc(s->packets, s->num_packets * sizeof(reorder_packet_t)); + s->packets = nut->alloc->realloc(s->packets, s->num_packets * sizeof(reorder_packet_t)); } static void flushcheck_frames(nut_context_t * nut) { @@ -67,7 +67,7 @@ flushcheck_frames(nut); for (i = 0; i < nut->stream_count; i++) { assert(!nut->sc[i].num_packets); - free(nut->sc[i].packets); + nut->alloc->free(nut->sc[i].packets); nut->sc[i].packets = NULL; } nut_muxer_uninit(nut); @@ -81,11 +81,11 @@ } s->num_packets++; - s->packets = realloc(s->packets, s->num_packets * sizeof(reorder_packet_t)); + s->packets = nut->alloc->realloc(s->packets, s->num_packets * sizeof(reorder_packet_t)); s->packets[s->num_packets - 1].p = *p; s->packets[s->num_packets - 1].dts = get_dts(s->sh.decode_delay, s->reorder_pts_cache, p->pts); - s->packets[s->num_packets - 1].buf = malloc(p->len); // FIXME + s->packets[s->num_packets - 1].buf = nut->alloc->malloc(p->len); // FIXME memcpy(s->packets[s->num_packets - 1].buf, buf, p->len); flushcheck_frames(nut);
participants (1)
-
ods15