comparison utils.c @ 1856:ed6eb3e304cc libavcodec

av_log() cleanup null pointer segfaults dont print redundant spam dont print prefix if reference==NULL class -> av_class dont copy AVClass to every object, its a waste of memory and not a good idea at all
author michael
date Wed, 03 Mar 2004 17:53:55 +0000
parents bafde44145f9
children ea2a4058441c
comparison
equal deleted inserted replaced
1855:bafde44145f9 1856:ed6eb3e304cc
340 340
341 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){ 341 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
342 return fmt[0]; 342 return fmt[0];
343 } 343 }
344 344
345 static const char* context_to_name(void* ptr) {
346 AVCodecContext *avc= ptr;
347
348 if(avc && avc->codec && avc->codec->name)
349 return avc->codec->name;
350 else
351 return "NULL";
352 }
353
354 static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
355
345 void avcodec_get_context_defaults(AVCodecContext *s){ 356 void avcodec_get_context_defaults(AVCodecContext *s){
346 memset(s, 0, sizeof(AVCodecContext)); 357 memset(s, 0, sizeof(AVCodecContext));
347 358
359 s->av_class= &av_codec_context_class;
348 s->bit_rate= 800*1000; 360 s->bit_rate= 800*1000;
349 s->bit_rate_tolerance= s->bit_rate*10; 361 s->bit_rate_tolerance= s->bit_rate*10;
350 s->qmin= 2; 362 s->qmin= 2;
351 s->qmax= 31; 363 s->qmax= 31;
352 s->mb_qmin= 2; 364 s->mb_qmin= 2;
384 396
385 /** 397 /**
386 * allocates a AVCodecContext and set it to defaults. 398 * allocates a AVCodecContext and set it to defaults.
387 * this can be deallocated by simply calling free() 399 * this can be deallocated by simply calling free()
388 */ 400 */
389 static const char* context_to_name(void* class_ptr) { return ((AVCodecContext*) class_ptr)->codec->name; }
390
391 static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
392
393 AVCodecContext *avcodec_alloc_context(void){ 401 AVCodecContext *avcodec_alloc_context(void){
394 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext)); 402 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
395 403
396 if(avctx==NULL) return NULL; 404 if(avctx==NULL) return NULL;
397 405
398 avctx->class = av_codec_context_class;
399 avcodec_get_context_defaults(avctx); 406 avcodec_get_context_defaults(avctx);
400 407
401 return avctx; 408 return avctx;
402 } 409 }
403 410
838 return ((h/c)<<32) + l/c; 845 return ((h/c)<<32) + l/c;
839 } 846 }
840 847
841 /* av_log API */ 848 /* av_log API */
842 849
843 static const char* null_to_name(void* class_ptr) { return "NULL"; }
844
845 static AVClass av_null_class = { "NULL", null_to_name };
846
847 static int av_log_level = AV_LOG_DEBUG; 850 static int av_log_level = AV_LOG_DEBUG;
848 851
849 static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) 852 static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
850 { 853 {
851 static int print_prefix=1; 854 static int print_prefix=1;
852 AVClass* avcl = ptr; 855 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
853 if(!avcl || !avcl->class_name)
854 avcl = &av_null_class;
855 if(level>av_log_level) 856 if(level>av_log_level)
856 return; 857 return;
857 #undef fprintf 858 #undef fprintf
858 if(print_prefix) { 859 if(print_prefix && avc) {
859 fprintf(stderr, "[%s:%s @ %p]", avcl->class_name, avcl->item_name(avcl), avcl); 860 fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
860 } 861 }
861 #define fprintf please_use_av_log 862 #define fprintf please_use_av_log
862 863
863 print_prefix= strstr(fmt, "\n") != NULL; 864 print_prefix= strstr(fmt, "\n") != NULL;
864 865