[MPlayer-dev-eng] [PATCH] allow builtin codecs table to be const

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Mon Dec 3 00:25:13 CET 2007


Hello,
I thought since the builtin codecs table is already several 100 kb it
would be nice if it was in .rodata.
Unfortunately there is a bit extra work necessary for that, since
currently one flag for each codec is stored in that table.
Attached patch makes the changes necessary.
What do you think?

Greetings,
Reimar Döffinger
-------------- next part --------------
Index: codec-cfg.c
===================================================================
--- codec-cfg.c	(revision 25275)
+++ codec-cfg.c	(working copy)
@@ -482,6 +482,8 @@
 
 static codecs_t *video_codecs=NULL;
 static codecs_t *audio_codecs=NULL;
+static uint32_t *selected_video = NULL;
+static uint32_t *selected_audio = NULL;
 static int nr_vcodecs = 0;
 static int nr_acodecs = 0;
 
@@ -508,6 +510,8 @@
 		audio_codecs = builtin_audio_codecs;
 		nr_vcodecs = sizeof(builtin_video_codecs)/sizeof(codecs_t);
 		nr_acodecs = sizeof(builtin_audio_codecs)/sizeof(codecs_t);
+		selected_video = calloc(4, nr_vcodecs >> 5);
+		selected_audio = calloc(4, nr_acodecs >> 5);
 		return 1;
 #endif
 	}
@@ -708,6 +712,8 @@
 	mp_msg(MSGT_CODECCFG,MSGL_INFO,MSGTR_AudioVideoCodecTotals, nr_acodecs, nr_vcodecs);
 	if(video_codecs) video_codecs[nr_vcodecs].name = NULL;
 	if(audio_codecs) audio_codecs[nr_acodecs].name = NULL;
+	selected_video = calloc(4, nr_vcodecs >> 5);
+	selected_audio = calloc(4, nr_acodecs >> 5);
 out:
 	free(line);
 	line=NULL;
@@ -751,6 +757,10 @@
 			}
 		if (codecs)
 			free(codecs);
+	free(selected_audio);
+	selected_audio = NULL;
+	free(selected_video);
+	selected_video = NULL;
 }
 
 void codecs_uninit_free(void) {
@@ -818,6 +828,42 @@
 	return NULL;
 }
 
+static int ptr2idx(const codecs_t *c, int audio) {
+	const codecs_t *start = audio ? audio_codecs : video_codecs;
+	int num = audio ? nr_acodecs : nr_vcodecs;
+	if (c < start || c > start + num - 1)
+		return -1;
+	return c - start;
+}
+
+void select_codec_ptr(const codecs_t *c) {
+	int offset;
+	uint32_t *field = selected_audio;
+	int index = ptr2idx(c, 1);
+	if (index < 0) {
+		field = selected_video;
+		index = ptr2idx(c, 0);
+	}
+	if (index < 0) return;
+	offset = index & 31;
+	index >>= 5;
+	field[index] |= 1 << offset;
+}
+
+int get_codec_ptr_selected(const codecs_t *c) {
+	int offset;
+	uint32_t *field = selected_audio;
+	int index = ptr2idx(c, 1);
+	if (index < 0) {
+		field = selected_video;
+		index = ptr2idx(c, 0);
+	}
+	if (index < 0) return -1;
+	offset = index & 31;
+	index >>= 5;
+	return (field[index] >> offset) & 1;
+}
+
 void select_codec(char* codecname,int audioflag){
 	int i;
 	codecs_t *c;
@@ -832,22 +878,14 @@
 	if(i)
 	for (/* NOTHING */; i--; c++)
 	    if(!strcmp(c->name,codecname))
-		c->flags|=CODECS_FLAG_SELECTED;
+		select_codec_ptr(c);
 }
 
 void codecs_reset_selection(int audioflag){
-	int i;
-	codecs_t *c;
-	if (audioflag) {
-		i = nr_acodecs;
-		c = audio_codecs;
-	} else {
-		i = nr_vcodecs;
-		c = video_codecs;
-	}
-	if(i)
-	for (/* NOTHING */; i--; c++)
-		c->flags&=(~CODECS_FLAG_SELECTED);
+	uint32_t *field = audioflag ? selected_audio : selected_video;
+	int cnt = audioflag ? nr_acodecs : nr_vcodecs;
+	cnt >>= 3;
+	memset(field, 0, cnt);
 }
 
 void list_codecs(int audioflag){
@@ -1028,7 +1066,7 @@
 		printf("/* GENERATED FROM %s, DO NOT EDIT! */\n\n",argv[1]);
 		
 		for (i=0; i<2; i++) {
-		  	printf("codecs_t %s[] = {\n", nm[i]);
+		  	printf("const codecs_t %s[] = {\n", nm[i]);
 			for (j = 0; j < nr[i]; j++) {
 			  	printf("{");
 
Index: codec-cfg.h
===================================================================
--- codec-cfg.h	(revision 25272)
+++ codec-cfg.h	(working copy)
@@ -10,7 +10,6 @@
 // Global flags:
 #define CODECS_FLAG_SEEKABLE	(1<<0)
 #define CODECS_FLAG_ALIGN16	(1<<1)
-#define CODECS_FLAG_SELECTED	(1<<15)  /* for internal use */
 
 // Outfmt flags:
 #define CODECS_FLAG_FLIP	(1<<0)
@@ -66,6 +65,8 @@
 codecs_t* find_codec(unsigned int fourcc, unsigned int *fourccmap,
                      codecs_t *start, int audioflag, int force);
 void select_codec(char* codecname,int audioflag);
+void select_codec_ptr(const codecs_t *c);
+int get_codec_ptr_selected(const codecs_t *c);
 void list_codecs(int audioflag);
 void codecs_reset_selection(int audioflag);
 void codecs_uninit_free(void);
Index: libmpcodecs/dec_audio.c
===================================================================
--- libmpcodecs/dec_audio.c	(revision 25272)
+++ libmpcodecs/dec_audio.c	(working copy)
@@ -152,7 +152,7 @@
 	if (sh_audio->wf)
 	    sh_audio->wf->wFormatTag = i;
 	// ok we found one codec
-	if (sh_audio->codec->flags & CODECS_FLAG_SELECTED)
+	if (get_codec_ptr_selected(sh_audio->codec))
 	    continue;	// already tried & failed
 	if (codecname && strcmp(sh_audio->codec->name, codecname))
 	    continue;	// -ac
@@ -160,7 +160,7 @@
 	    continue;	// afm doesn't match
 	if (!force && sh_audio->codec->status < status)
 	    continue;	// too unstable
-	sh_audio->codec->flags |= CODECS_FLAG_SELECTED;	// tagging it
+	select_codec_ptr(sh_audio->codec);	// tagging it
 	// ok, it matches all rules, let's find the driver!
 	for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
 	    if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
Index: libmpcodecs/dec_video.c
===================================================================
--- libmpcodecs/dec_video.c	(revision 25272)
+++ libmpcodecs/dec_video.c	(working copy)
@@ -194,11 +194,11 @@
           sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,
           sh_video->codec,force) )) break;
 	// ok we found one codec
-	if(sh_video->codec->flags&CODECS_FLAG_SELECTED) continue; // already tried & failed
+	if(get_codec_ptr_selected(sh_video->codec)) continue; // already tried & failed
 	if(codecname && strcmp(sh_video->codec->name,codecname)) continue; // -vc
 	if(vfm && strcmp(sh_video->codec->drv,vfm)) continue; // vfm doesn't match
 	if(!force && sh_video->codec->status<status) continue; // too unstable
-	sh_video->codec->flags|=CODECS_FLAG_SELECTED; // tagging it
+	select_codec_ptr(sh_video->codec); // tagging it
 	// ok, it matches all rules, let's find the driver!
 	for (i=0; mpcodecs_vd_drivers[i] != NULL; i++)
 //	    if(mpcodecs_vd_drivers[i]->info->id==sh_video->codec->driver) break;


More information about the MPlayer-dev-eng mailing list