comparison libswscale/swscale.c @ 29542:3e7ffd70b12b

swscale: Check for return values of malloc.
author ramiro
date Sat, 29 Aug 2009 23:02:01 +0000
parents 0f86c85da42a
children c7249524c681
comparison
equal deleted inserted replaced
29541:b5a8ade9175b 29542:3e7ffd70b12b
74 #include "swscale.h" 74 #include "swscale.h"
75 #include "swscale_internal.h" 75 #include "swscale_internal.h"
76 #include "rgb2rgb.h" 76 #include "rgb2rgb.h"
77 #include "libavutil/intreadwrite.h" 77 #include "libavutil/intreadwrite.h"
78 #include "libavutil/x86_cpu.h" 78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/avutil.h"
79 #include "libavutil/bswap.h" 80 #include "libavutil/bswap.h"
80 81
81 unsigned swscale_version(void) 82 unsigned swscale_version(void)
82 { 83 {
83 return LIBSWSCALE_VERSION_INT; 84 return LIBSWSCALE_VERSION_INT;
1448 if (flags & SWS_CPU_CAPS_MMX) 1449 if (flags & SWS_CPU_CAPS_MMX)
1449 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions) 1450 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1450 #endif 1451 #endif
1451 1452
1452 // NOTE: the +1 is for the MMX scaler which reads over the end 1453 // NOTE: the +1 is for the MMX scaler which reads over the end
1453 *filterPos = av_malloc((dstW+1)*sizeof(int16_t)); 1454 CHECKED_ALLOC(*filterPos, (dstW+1)*sizeof(int16_t));
1454 1455
1455 if (FFABS(xInc - 0x10000) <10) { // unscaled 1456 if (FFABS(xInc - 0x10000) <10) { // unscaled
1456 int i; 1457 int i;
1457 filterSize= 1; 1458 filterSize= 1;
1458 filter= av_mallocz(dstW*sizeof(*filter)*filterSize); 1459 CHECKED_ALLOCZ(filter, dstW*sizeof(*filter)*filterSize);
1459 1460
1460 for (i=0; i<dstW; i++) { 1461 for (i=0; i<dstW; i++) {
1461 filter[i*filterSize]= fone; 1462 filter[i*filterSize]= fone;
1462 (*filterPos)[i]=i; 1463 (*filterPos)[i]=i;
1463 } 1464 }
1464 1465
1465 } else if (flags&SWS_POINT) { // lame looking point sampling mode 1466 } else if (flags&SWS_POINT) { // lame looking point sampling mode
1466 int i; 1467 int i;
1467 int xDstInSrc; 1468 int xDstInSrc;
1468 filterSize= 1; 1469 filterSize= 1;
1469 filter= av_malloc(dstW*sizeof(*filter)*filterSize); 1470 CHECKED_ALLOC(filter, dstW*sizeof(*filter)*filterSize);
1470 1471
1471 xDstInSrc= xInc/2 - 0x8000; 1472 xDstInSrc= xInc/2 - 0x8000;
1472 for (i=0; i<dstW; i++) { 1473 for (i=0; i<dstW; i++) {
1473 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16; 1474 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1474 1475
1478 } 1479 }
1479 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale 1480 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1480 int i; 1481 int i;
1481 int xDstInSrc; 1482 int xDstInSrc;
1482 filterSize= 2; 1483 filterSize= 2;
1483 filter= av_malloc(dstW*sizeof(*filter)*filterSize); 1484 CHECKED_ALLOC(filter, dstW*sizeof(*filter)*filterSize);
1484 1485
1485 xDstInSrc= xInc/2 - 0x8000; 1486 xDstInSrc= xInc/2 - 0x8000;
1486 for (i=0; i<dstW; i++) { 1487 for (i=0; i<dstW; i++) {
1487 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16; 1488 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1488 int j; 1489 int j;
1517 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale 1518 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1518 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW; 1519 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1519 1520
1520 if (filterSize > srcW-2) filterSize=srcW-2; 1521 if (filterSize > srcW-2) filterSize=srcW-2;
1521 1522
1522 filter= av_malloc(dstW*sizeof(*filter)*filterSize); 1523 CHECKED_ALLOC(filter, dstW*sizeof(*filter)*filterSize);
1523 1524
1524 xDstInSrc= xInc - 0x10000; 1525 xDstInSrc= xInc - 0x10000;
1525 for (i=0; i<dstW; i++) { 1526 for (i=0; i<dstW; i++) {
1526 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17); 1527 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1527 int j; 1528 int j;
1605 assert(filterSize>0); 1606 assert(filterSize>0);
1606 filter2Size= filterSize; 1607 filter2Size= filterSize;
1607 if (srcFilter) filter2Size+= srcFilter->length - 1; 1608 if (srcFilter) filter2Size+= srcFilter->length - 1;
1608 if (dstFilter) filter2Size+= dstFilter->length - 1; 1609 if (dstFilter) filter2Size+= dstFilter->length - 1;
1609 assert(filter2Size>0); 1610 assert(filter2Size>0);
1610 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2)); 1611 CHECKED_ALLOCZ(filter2, filter2Size*dstW*sizeof(*filter2));
1611 1612
1612 for (i=0; i<dstW; i++) { 1613 for (i=0; i<dstW; i++) {
1613 int j, k; 1614 int j, k;
1614 1615
1615 if(srcFilter) { 1616 if(srcFilter) {
1688 assert(minFilterSize > 0); 1689 assert(minFilterSize > 0);
1689 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1)); 1690 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1690 assert(filterSize > 0); 1691 assert(filterSize > 0);
1691 filter= av_malloc(filterSize*dstW*sizeof(*filter)); 1692 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1692 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter) 1693 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1693 goto error; 1694 goto fail;
1694 *outFilterSize= filterSize; 1695 *outFilterSize= filterSize;
1695 1696
1696 if (flags&SWS_PRINT_INFO) 1697 if (flags&SWS_PRINT_INFO)
1697 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize); 1698 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1698 /* try to reduce the filter-size (step2 reduce it) */ 1699 /* try to reduce the filter-size (step2 reduce it) */
1735 } 1736 }
1736 } 1737 }
1737 1738
1738 // Note the +1 is for the MMX scaler which reads over the end 1739 // Note the +1 is for the MMX scaler which reads over the end
1739 /* align at 16 for AltiVec (needed by hScale_altivec_real) */ 1740 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1740 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t)); 1741 CHECKED_ALLOCZ(*outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t));
1741 1742
1742 /* normalize & store in outFilter */ 1743 /* normalize & store in outFilter */
1743 for (i=0; i<dstW; i++) { 1744 for (i=0; i<dstW; i++) {
1744 int j; 1745 int j;
1745 int64_t error=0; 1746 int64_t error=0;
1762 int j= dstW*(*outFilterSize); 1763 int j= dstW*(*outFilterSize);
1763 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)]; 1764 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1764 } 1765 }
1765 1766
1766 ret=0; 1767 ret=0;
1767 error: 1768 fail:
1768 av_free(filter); 1769 av_free(filter);
1769 av_free(filter2); 1770 av_free(filter2);
1770 return ret; 1771 return ret;
1771 } 1772 }
1772 1773
2596 } 2597 }
2597 2598
2598 if (!dstFilter) dstFilter= &dummyFilter; 2599 if (!dstFilter) dstFilter= &dummyFilter;
2599 if (!srcFilter) srcFilter= &dummyFilter; 2600 if (!srcFilter) srcFilter= &dummyFilter;
2600 2601
2601 c= av_mallocz(sizeof(SwsContext)); 2602 CHECKED_ALLOCZ(c, sizeof(SwsContext));
2602 2603
2603 c->av_class = &sws_context_class; 2604 c->av_class = &sws_context_class;
2604 c->srcW= srcW; 2605 c->srcW= srcW;
2605 c->srcH= srcH; 2606 c->srcH= srcH;
2606 c->dstW= dstW; 2607 c->dstW= dstW;
2835 #else 2836 #else
2836 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize); 2837 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2837 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize); 2838 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2838 #endif 2839 #endif
2839 2840
2840 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t)); 2841 CHECKED_ALLOCZ(c->lumMmx2Filter , (dstW /8+8)*sizeof(int16_t));
2841 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t)); 2842 CHECKED_ALLOCZ(c->chrMmx2Filter , (c->chrDstW /4+8)*sizeof(int16_t));
2842 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t)); 2843 CHECKED_ALLOCZ(c->lumMmx2FilterPos, (dstW /2/8+8)*sizeof(int32_t));
2843 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t)); 2844 CHECKED_ALLOCZ(c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t));
2844 2845
2845 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8); 2846 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2846 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4); 2847 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2847 2848
2848 #ifdef MAP_ANONYMOUS 2849 #ifdef MAP_ANONYMOUS
2870 c->chrSrcH, c->chrDstH, filterAlign, (1<<12), 2871 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2871 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, 2872 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2872 srcFilter->chrV, dstFilter->chrV, c->param); 2873 srcFilter->chrV, dstFilter->chrV, c->param);
2873 2874
2874 #ifdef COMPILE_ALTIVEC 2875 #ifdef COMPILE_ALTIVEC
2875 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH); 2876 CHECKED_ALLOC(c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2876 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH); 2877 CHECKED_ALLOC(c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2877 2878
2878 for (i=0;i<c->vLumFilterSize*c->dstH;i++) { 2879 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2879 int j; 2880 int j;
2880 short *p = (short *)&c->vYCoeffsBank[i]; 2881 short *p = (short *)&c->vYCoeffsBank[i];
2881 for (j=0;j<8;j++) 2882 for (j=0;j<8;j++)
2907 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI]; 2908 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2908 } 2909 }
2909 2910
2910 // allocate pixbufs (we use dynamic allocation because otherwise we would need to 2911 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2911 // allocate several megabytes to handle all possible cases) 2912 // allocate several megabytes to handle all possible cases)
2912 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*)); 2913 CHECKED_ALLOC(c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*));
2913 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*)); 2914 CHECKED_ALLOC(c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*));
2914 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) 2915 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2915 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*)); 2916 CHECKED_ALLOCZ(c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*));
2916 //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000) 2917 //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
2917 /* align at 16 bytes for AltiVec */ 2918 /* align at 16 bytes for AltiVec */
2918 for (i=0; i<c->vLumBufSize; i++) 2919 for (i=0; i<c->vLumBufSize; i++)
2919 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1); 2920 {
2921 CHECKED_ALLOCZ(c->lumPixBuf[i+c->vLumBufSize], VOF+1);
2922 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2923 }
2920 for (i=0; i<c->vChrBufSize; i++) 2924 for (i=0; i<c->vChrBufSize; i++)
2921 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2); 2925 {
2926 CHECKED_ALLOC(c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2);
2927 c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2928 }
2922 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) 2929 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2923 for (i=0; i<c->vLumBufSize; i++) 2930 for (i=0; i<c->vLumBufSize; i++)
2924 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1); 2931 {
2932 CHECKED_ALLOCZ(c->alpPixBuf[i+c->vLumBufSize], VOF+1);
2933 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2934 }
2925 2935
2926 //try to avoid drawing green stuff between the right end and the stride end 2936 //try to avoid drawing green stuff between the right end and the stride end
2927 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2); 2937 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2928 2938
2929 assert(2*VOFW == VOF); 2939 assert(2*VOFW == VOF);
3043 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc); 3053 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3044 } 3054 }
3045 3055
3046 c->swScale= getSwsFunc(c); 3056 c->swScale= getSwsFunc(c);
3047 return c; 3057 return c;
3058
3059 fail:
3060 sws_freeContext(c);
3061 return NULL;
3048 } 3062 }
3049 3063
3050 static void reset_ptr(uint8_t* src[], int format) 3064 static void reset_ptr(uint8_t* src[], int format)
3051 { 3065 {
3052 if(!isALPHA(format)) 3066 if(!isALPHA(format))
3187 float lumaSharpen, float chromaSharpen, 3201 float lumaSharpen, float chromaSharpen,
3188 float chromaHShift, float chromaVShift, 3202 float chromaHShift, float chromaVShift,
3189 int verbose) 3203 int verbose)
3190 { 3204 {
3191 SwsFilter *filter= av_malloc(sizeof(SwsFilter)); 3205 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3206 if (!filter)
3207 return NULL;
3192 3208
3193 if (lumaGBlur!=0.0) { 3209 if (lumaGBlur!=0.0) {
3194 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0); 3210 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3195 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0); 3211 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3196 } else { 3212 } else {