Re: Solution for segfault when using export_dv

"Francesco Romani" <[email protected]>
Newsgroups gmane.comp.video.transcode.devel
Message-ID <[email protected]>
On 10/24/07, Kees Bakker <[email protected]> wrote:
>
> Hi guys,
>
> On my system transcode gives a segfault when export to dv.
> After analyzing the problem I found to cause to be here:
>

Hi, can you please test the attached patch? It's against vanilla
1.0.5rc2but it should apply against
1.0.4 too.

Thanks and best regards,

-- 
Francesco Romani // Ikitt
tc-ex-dv-bufalloc.patch (text/x-patch, 5.6 KB)
Index: export/export_dv.c
===================================================================
RCS file: /cvstc/transcode/export/export_dv.c,v
retrieving revision 1.10
diff -r1.10 export_dv.c
53,77d52
< static unsigned char *bufalloc(size_t size)
< {
< 
< #ifdef HAVE_GETPAGESIZE
<    long buffer_align=getpagesize();
< #else
<    long buffer_align=0;
< #endif
< 
<    char *buf = malloc(size + buffer_align);
< 
<    long adjust;
< 
<    if (buf == NULL) {
<        fprintf(stderr, "(%s) out of memory", __FILE__);
<    }
<    
<    adjust = buffer_align - ((long) buf) % buffer_align;
< 
<    if (adjust == buffer_align)
<       adjust = 0;
< 
<    return (unsigned char *) (buf + adjust);
< }
< 
90c65
<       target = bufalloc(TC_FRAME_DV_PAL);
---
>       target = tc_bufalloc(TC_FRAME_DV_PAL);
93,94c68,69
< 	tmp_buf = bufalloc(PAL_W*PAL_H*2); //max frame
< 	dv_yuy2_mode=1;
---
>     	tmp_buf = tc_bufalloc(PAL_W*PAL_H*2); //max frame
> 	    dv_yuy2_mode=1;
Index: export/export_dvraw.c
===================================================================
RCS file: /cvstc/transcode/export/export_dvraw.c,v
retrieving revision 1.27.2.1
diff -r1.27.2.1 export_dvraw.c
58,122d57
< static unsigned char *bufalloc(size_t size)
< {
< 
< #ifdef HAVE_GETPAGESIZE
<    long buffer_align=getpagesize();
< #else
<    long buffer_align=0;
< #endif
< 
<    char *buf = malloc(size + buffer_align);
< 
<    long adjust;
< 
<    if (buf == NULL) {
<        fprintf(stderr, "(%s) out of memory", __FILE__);
<    }
<    
<    adjust = buffer_align - ((long) buf) % buffer_align;
< 
<    if (adjust == buffer_align)
<       adjust = 0;
< 
<    return (unsigned char *) (buf + adjust);
< }
< 
< #if 0  /* get this from ioaux.c */
< static int p_write (int fd, char *buf, size_t len)
< {
<    size_t n = 0;
<    size_t r = 0;
< 
<    while (r < len) {
<       n = write (fd, buf + r, len - r);
<       if (n < 0)
<          return n;
<       
<       r += n;
<    }
<    return r;
< }
< #endif
< 
< #if 0
< static void pcm_swap(char *buffer, int len)
< {
<   char *in, *out;
< 
<   int n;
< 
<   char tt;
< 
<   in  = buffer;
<   out = buffer;
< 
<   for(n=0; n<len; n=n+2) {
< 
<     tt = *(in+1);
<     *(out+1) = *in;
<     *out = tt;
<     
<     in = in+2;
<     out = out+2;
<   }
< }
< #endif
137,138c72,73
<     target = bufalloc(TC_FRAME_DV_PAL);
<     vbuf = bufalloc(PAL_W*PAL_H*3);
---
>     target = tc_bufalloc(TC_FRAME_DV_PAL);
>     vbuf = tc_bufalloc(PAL_W*PAL_H*3);
141c76
<       tmp_buf = bufalloc(PAL_W*PAL_H*2); //max frame
---
>       tmp_buf = tc_bufalloc(PAL_W*PAL_H*2); //max frame
146c81
<       tmp_buf = bufalloc(PAL_W*PAL_H*2); //max frame
---
>       tmp_buf = tc_bufalloc(PAL_W*PAL_H*2); //max frame
Index: libtc/libtc.h
===================================================================
RCS file: /cvstc/transcode/libtc/libtc.h,v
retrieving revision 1.5.2.1
diff -r1.5.2.1 libtc.h
87a88,130
> /*
>  * Allocate a buffer aligned to the machine's page size, if known.  The
>  * buffer must be freed with buffree() (not free()).
>  */
> 
> #define tc_bufalloc(size) \
>             _tc_bufalloc(__FILE__, __LINE__, size)
> 
> /*
>  * _tc_bufalloc:
>  *     do the real work behind _tc_bufalloc macro
>  *
>  * Parameters:
>  *     file: name of the file on which call occurs
>  *     line: line of above file on which call occurs
>  *           (above two parameters are intended to be, and usually
>  *           are, filled by tc_malloc macro)
>  *     size: size of desired chunk of memory
>  * Return Value:
>  *     a pointer of acquired, aligned, memory, or NULL if acquisition fails
>  * Side effects:
>  *     a message is printed on stderr (20051017)
>  * Preconditions:
>  *     file param not null
>  */
> void *_tc_bufalloc(const char *file, int line, size_t size);
> 
> /*
>  * tc_buffree:
>  *     release a memory buffer acquired using tc_bufalloc
>  *
>  * Parameters:
>  *     ptr: pointer obtained as return value of a succesfull
>  *          tc_bufalloc() call
>  * Return Value:
>  *     none
>  * Preconditions:
>  *     ptr is acquired via tc_bufalloc(). Really BAD things will happen
>  *     if a buffer acquired via tc_bufalloc() is released using anything
>  *     but tc_buffree(), or vice versa.
>  */
> void tc_buffree(void *ptr);
> 
Index: libtc/tc_functions.c
===================================================================
RCS file: /cvstc/transcode/libtc/tc_functions.c,v
retrieving revision 1.6.2.1
diff -r1.6.2.1 tc_functions.c
247a248,289
> 
> /*************************************************************************/
> /* Allocate a buffer aligned to the machine's page size, if known.  The
>  * buffer must be freed with buffree() (not free()). */
> 
> void *_tc_bufalloc(const char *file, int line, size_t size)
> {
> #ifdef HAVE_GETPAGESIZE
>     unsigned long pagesize = getpagesize();
>     int8_t *base = malloc(size + sizeof(void *) + pagesize);
>     int8_t *ptr = NULL;
>     unsigned long offset = 0;
> 
>     if (base == NULL) {
>         fprintf(stderr, "[%s:%d] tc_bufalloc(): can't allocate %lu bytes\n",
>                         file, line, (unsigned long)size);
>     } else {
>         ptr = base + sizeof(void *);
>         offset = (unsigned long)ptr % pagesize;
> 
>         if (offset)
>             ptr += (pagesize - offset);
>         ((void **)ptr)[-1] = base;  /* save the base pointer for freeing */
>     }
>     return ptr;
> #else  /* !HAVE_GETPAGESIZE */
>     return malloc(size);
> #endif
> }
> 
> /* Free a buffer allocated with tc_bufalloc(). */
> void tc_buffree(void *ptr)
> {
> #ifdef HAVE_GETPAGESIZE
>     if (ptr)
> 	free(((void **)ptr)[-1]);
> #else
>     free(ptr);
> #endif
> }
> 
> /*************************************************************************/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.