[NUT-devel] [NUT] (ods15): r92 - in /trunk/libnut: demuxer.c muxer.c

syncmail at mplayerhq.hu syncmail at mplayerhq.hu
Sat Mar 11 13:49:12 CET 2006


Author: ods15
Date: Sat Mar 11 13:49:12 2006
New Revision: 92

Log:
sync to spec, syncpoints are their own headers
some more asserts
small bugfix in demuxer, ptr could be invalid in find_syncpoint
less waste in demuxer with no unnecessary memcpy and malloc

Modified:
    trunk/libnut/demuxer.c
    trunk/libnut/muxer.c

Modified: trunk/libnut/demuxer.c
==============================================================================
--- trunk/libnut/demuxer.c (original)
+++ trunk/libnut/demuxer.c Sat Mar 11 13:49:12 2006
@@ -77,8 +77,7 @@
 	return bc->buf + start;
 }
 
-static input_buffer_t * new_mem_buffer() {
-	input_buffer_t * bc = malloc(sizeof(input_buffer_t));
+static input_buffer_t * new_mem_buffer(input_buffer_t * bc) {
 	bc->read_len = 0;
 	bc->write_len = 0;
 	bc->is_mem = 1;
@@ -89,7 +88,7 @@
 }
 
 static input_buffer_t * new_input_buffer(nut_input_stream_t isc) {
-	input_buffer_t * bc = new_mem_buffer();
+	input_buffer_t * bc = new_mem_buffer(malloc(sizeof(input_buffer_t)));
 	bc->is_mem = 0;
 	bc->isc = isc;
 	bc->file_pos = isc.file_pos;
@@ -102,6 +101,7 @@
 }
 
 static void free_buffer(input_buffer_t * bc) {
+	assert(!bc->is_mem);
 	if (!bc) return;
 	free(bc->buf);
 	free(bc);
@@ -210,19 +210,15 @@
 	if (out) {
 		assert(out->is_mem);
 		assert(out->buf == out->buf_ptr);
-		if (out->write_len < forward_ptr - 4) {
-			out->write_len = forward_ptr - 4;
-			out->buf_ptr = out->buf = realloc(out->buf, out->write_len);
-		}
-		memcpy(out->buf, in->buf_ptr - forward_ptr, forward_ptr - 4);
-		out->read_len = forward_ptr - 4; // not including checksum
+		out->buf_ptr = out->buf = in->buf_ptr - forward_ptr;
+		out->write_len = out->read_len = forward_ptr - 4; // not including checksum
 	}
 err_out:
 	return err;
 }
 
 static int get_main_header(nut_context_t * nut) {
-	input_buffer_t * tmp = new_mem_buffer();
+	input_buffer_t itmp, * tmp = new_mem_buffer(&itmp);
 	int i, j, err = 0;
 	int flag, fields, timestamp = 0, mul = 1, stream = 0, sflag, size, count, reserved;
 
@@ -270,12 +266,11 @@
 		}
 	}
 err_out:
-	free_buffer(tmp);
 	return err;
 }
 
 static int get_stream_header(nut_context_t * nut, int id) {
-	input_buffer_t * tmp = new_mem_buffer();
+	input_buffer_t itmp, * tmp = new_mem_buffer(&itmp);
 	stream_context_t * sc = &nut->sc[id];
 	int i, err = 0;
 	uint64_t a;
@@ -311,7 +306,6 @@
 			break;
 	}
 err_out:
-	free_buffer(tmp);
 	return err;
 }
 
@@ -372,11 +366,14 @@
 	int err = 0;
 	syncpoint_t s;
 	int after_seek = nut->last_syncpoint ? 0 : 1;
+	input_buffer_t itmp, * tmp = new_mem_buffer(&itmp);
 
 	nut->last_syncpoint = s.pos = bctello(nut->i) - 8;
 
-	GET_V(nut->i, s.pts);
-	GET_V(nut->i, s.back_ptr);
+	CHECK(get_header(nut->i, tmp));
+
+	GET_V(tmp, s.pts);
+	GET_V(tmp, s.back_ptr);
 	s.back_ptr = (s.back_ptr * 8 + 7)<<1;
 
 	set_global_pts(nut, s.pts);
@@ -404,7 +401,7 @@
 }
 
 static int get_index(nut_context_t * nut) {
-	input_buffer_t * tmp = new_mem_buffer();
+	input_buffer_t itmp, * tmp = new_mem_buffer(&itmp);
 	int err = 0;
 	syncpoint_list_t * sl = &nut->syncpoints;
 	uint64_t x;
@@ -473,7 +470,6 @@
 	fprintf(stderr, "NUT index read successfully, %d syncpoints\n", sl->len);
 
 err_out:
-	free_buffer(tmp);
 	return err;
 }
 
@@ -496,37 +492,13 @@
 	if (tmp == 'N') {
 		CHECK(get_bytes(nut->i, 7, &tmp));
 		tmp |= (uint64_t)'N' << 56;
-		if (tmp == MAIN_STARTCODE) {
-			int i;
-			GET_V(nut->i, pd->len);
-			CHECK(skip_buffer(nut->i, pd->len));
-			for (i = 0; i < nut->stream_count; i++) {
-				CHECK(get_bytes(nut->i, 8, &tmp));
-				ERROR(tmp != STREAM_STARTCODE, -ERR_NOSTREAM_STARTCODE);
-				GET_V(nut->i, pd->len);
-				CHECK(skip_buffer(nut->i, pd->len));
-			}
-			CHECK(get_bytes(nut->i, 8, &tmp));
-			while (tmp == INFO_STARTCODE) {
-				GET_V(nut->i, pd->len);
-				CHECK(skip_buffer(nut->i, pd->len));
-				CHECK(get_bytes(nut->i, 8, &tmp));
-			}
-			if (tmp == INDEX_STARTCODE) {
-				GET_V(nut->i, pd->len);
-				CHECK(skip_buffer(nut->i, pd->len));
-				CHECK(get_bytes(nut->i, 8, &tmp));
-			}
-			ERROR(tmp != SYNCPOINT_STARTCODE && tmp != MAIN_STARTCODE, -ERR_NO_HEADERS);
-			nut->i->buf_ptr -= 8;
-			return get_packet(nut, pd, saw_syncpoint);
-		} else if (tmp == SYNCPOINT_STARTCODE) {
+		if (tmp == SYNCPOINT_STARTCODE) {
 			after_sync = bctello(nut->i);
 			CHECK(get_syncpoint(nut));
 			CHECK(get_bytes(nut->i, 1, &tmp));
 		} else {
-			nut->i->buf_ptr -= 7;
-			tmp = 'N';
+			CHECK(get_header(nut->i, NULL));
+			return get_packet(nut, pd, saw_syncpoint);
 		}
 	}
 
@@ -560,7 +532,7 @@
 
 	for (i = 0; i < nut->ft[tmp].reserved; i++) { int scrap; GET_V(nut->i, scrap); }
 
-	if (after_sync) {
+	if (0) {
 		uint64_t checksum;
 		off_t pos = bctello(nut->i);
 		CHECK(get_bytes(nut->i, 4, &checksum));
@@ -599,7 +571,7 @@
 	int read;
 	int err = 0;
 	uint64_t tmp;
-	uint8_t * ptr = NULL;
+	off_t ptr = 0;
 	assert(!backwards || !stop); // can't have both
 
 	if (backwards) seek_buf(nut->i, -nut->max_distance, SEEK_CUR);
@@ -614,32 +586,22 @@
 		tmp = (tmp << 8) | *(nut->i->buf_ptr++);
 		if (tmp != SYNCPOINT_STARTCODE) continue;
 		if (res) {
-			int i, scrap;
+			input_buffer_t itmp, * tmp = new_mem_buffer(&itmp);
 			res->pos = bctello(nut->i) - 8;
-			GET_V(nut->i, res->pts);
-			GET_V(nut->i, tmp);
-			res->back_ptr = (tmp * 8 + 7) << 1;
-
-			CHECK(get_bytes(nut->i, 1, &tmp));
-			if (!nut->ft[tmp].stream_plus1) GET_V(nut->i, scrap);
-			if (!nut->ft[tmp].pts_delta) GET_V(nut->i, scrap);
-			if (nut->ft[tmp].flags & MSB_CODED_FLAG) GET_V(nut->i, scrap);
-			if (nut->ft[tmp].flags & STREAM_CODED_FLAG) GET_V(nut->i, scrap);
-			for (i = 0; i < nut->ft[tmp].reserved; i++) GET_V(nut->i, scrap);
-			CHECK(get_bytes(nut->i, 4, &tmp));
-
-			if (tmp != crc32(nut->i->buf_ptr - (bctello(nut->i) - res->pos - 8), bctello(nut->i) - 4 - res->pos - 8)) {
-				tmp = 0;
-				nut->i->buf_ptr -= bctello(nut->i) - res->pos - 8; // rewind to right after the startcode
-				continue;
-			}
+
+			if ((err = get_header(nut->i, tmp)) == 2) goto err_out;
+			if (err) { err = 0; continue; }
+
+			GET_V(tmp, res->pts);
+			GET_V(tmp, res->back_ptr);
+			res->back_ptr = (res->back_ptr * 8 + 7) << 1;
 		}
 		if (!backwards) return 0;
-		else ptr = nut->i->buf_ptr;
+		else ptr = bctello(nut->i);
 	}
 
 	if (ptr) {
-		nut->i->buf_ptr = ptr;
+		nut->i->buf_ptr -= bctello(nut->i) - ptr;
 		return 0;
 	}
 

Modified: trunk/libnut/muxer.c
==============================================================================
--- trunk/libnut/muxer.c (original)
+++ trunk/libnut/muxer.c Sat Mar 11 13:49:12 2006
@@ -57,6 +57,7 @@
 }
 
 static output_buffer_t * clear_buffer(output_buffer_t * bc) {
+	assert(bc->is_mem);
 	bc->buf_ptr = bc->buf;
 	return bc;
 }
@@ -258,7 +259,8 @@
 	for (i = 0; i < nut->info_count; i++) put_info(nut, &nut->info[i]);
 }
 
-static void put_syncpoint(nut_context_t * nut, output_buffer_t * bc) {
+static void put_syncpoint(nut_context_t * nut) {
+	output_buffer_t * tmp = clear_buffer(nut->tmp_buffer);
 	int i;
 	uint64_t pts = 0;
 	int stream = 0;
@@ -309,11 +311,12 @@
 		if (nut->sc[i].eor) nut->sc[i].eor = -1; // so we know to ignore this stream in future syncpoints
 	}
 
-	put_bytes(bc, 8, SYNCPOINT_STARTCODE);
-	put_v(bc, pts * nut->stream_count + stream);
-	put_v(bc, back_ptr);
-
-	nut->sync_overhead += bctello(bc) + 4/*checksum*/;
+	put_v(tmp, pts * nut->stream_count + stream);
+	put_v(tmp, back_ptr);
+
+	put_header(nut->o, tmp, nut->tmp_buffer2, SYNCPOINT_STARTCODE, 0);
+
+	nut->sync_overhead += bctello(tmp) + bctello(nut->tmp_buffer2);
 }
 
 static void put_index(nut_context_t * nut) {
@@ -414,42 +417,31 @@
 	return size;
 }
 
-static int put_frame_header(nut_context_t * nut, output_buffer_t * bc, const nut_packet_t * fd) {
+static void put_frame(nut_context_t * nut, const nut_packet_t * fd, const uint8_t * data) {
+	output_buffer_t * tmp = clear_buffer(nut->tmp_buffer);
 	stream_context_t * sc = &nut->sc[fd->stream];
 	int ftnum = -1, coded_pts, pts_delta = fd->pts - sc->last_pts;
-	int size;
+	int i;
 
 	if (ABS(pts_delta) < (1 << (sc->msb_pts_shift - 1)) - 1)
 		coded_pts = fd->pts & ((1 << sc->msb_pts_shift) - 1);
 	else
 		coded_pts = fd->pts + (1 << sc->msb_pts_shift);
 
-	size = frame_header(nut, fd, &ftnum);
-
-	put_bytes(bc, 1, ftnum); // frame_code
-
-	if (!nut->ft[ftnum].stream_plus1) put_v(bc, fd->stream);
-	if (!nut->ft[ftnum].pts_delta)    put_v(bc, coded_pts);
+	sc->overhead += frame_header(nut, fd, &ftnum);
+
+	put_bytes(tmp, 1, ftnum); // frame_code
+
+	if (!nut->ft[ftnum].stream_plus1) put_v(tmp, fd->stream);
+	if (!nut->ft[ftnum].pts_delta)    put_v(tmp, coded_pts);
 	if (nut->ft[ftnum].flags & MSB_CODED_FLAG)
-		put_v(bc, (fd->len - nut->ft[ftnum].lsb) / nut->ft[ftnum].mul);
+		put_v(tmp, (fd->len - nut->ft[ftnum].lsb) / nut->ft[ftnum].mul);
 	if (nut->ft[ftnum].flags & STREAM_CODED_FLAG)
-		put_v(bc, (fd->flags & 3) ^ nut->ft[ftnum].stream_flags);
-
-	return size;
-}
-
-static void put_frame(nut_context_t * nut, const nut_packet_t * fd, const uint8_t * data, int write_syncpoint) {
-	output_buffer_t * tmp = clear_buffer(nut->tmp_buffer);
-	stream_context_t * sc = &nut->sc[fd->stream];
-	int i;
-
-	if (write_syncpoint) put_syncpoint(nut, tmp);
-
-	sc->overhead += put_frame_header(nut, tmp, fd);
-
-	put_data(nut->o, tmp->buf_ptr - tmp->buf, tmp->buf);
-
-	if (write_syncpoint) put_bytes(nut->o, 4, crc32(tmp->buf + 8, tmp->buf_ptr - tmp->buf - 8)); // not including startcode
+		put_v(tmp, (fd->flags & 3) ^ nut->ft[ftnum].stream_flags);
+
+	if (0) put_bytes(tmp, 4, crc32(tmp->buf + 8, tmp->buf_ptr - tmp->buf - 8)); // not including startcode
+
+	put_data(nut->o, bctello(tmp), tmp->buf);
 
 	sc->total_frames++;
 	sc->tot_size += fd->len;
@@ -471,7 +463,6 @@
 
 void nut_write_frame(nut_context_t * nut, const nut_packet_t * fd, const uint8_t * buf) {
 	stream_context_t * sc = &nut->sc[fd->stream];
-	int write_syncpoint = 0;
 
 	if (bctello(nut->o) > (1 << 23)) { // main header repetition
 		int i = 23; // ### magic value for header repetition
@@ -487,9 +478,9 @@
 	if (ABS((int64_t)fd->pts - (int64_t)sc->last_pts) > sc->max_pts_distance)
 		fprintf(stderr, "%d - %d > %d   \n", (int)fd->pts, (int)sc->last_pts, sc->max_pts_distance);
 	if (nut->last_syncpoint < nut->last_headers || ABS((int64_t)fd->pts - (int64_t)sc->last_pts) > sc->max_pts_distance ||
-		bctello(nut->o) - nut->last_syncpoint + fd->len + frame_header(nut, fd, NULL) > nut->max_distance) write_syncpoint = 1;
-
-	put_frame(nut, fd, buf, write_syncpoint);
+		bctello(nut->o) - nut->last_syncpoint + fd->len + frame_header(nut, fd, NULL) > nut->max_distance) put_syncpoint(nut);
+
+	put_frame(nut, fd, buf);
 
 	if ((fd->flags & NUT_KEY_STREAM_FLAG) && !sc->last_key) sc->last_key = fd->pts + 1;
 	if (fd->flags & NUT_EOR_STREAM_FLAG) sc->eor = fd->pts + 1;




More information about the NUT-devel mailing list