The attached diff modifies the code samples as follows:
* Allocate aligned buffers using mpeg2_malloc(), to avoid reimplementing
this in the samples (and demonstrate usage - mpeg2_malloc() is public
just so people can do this)
* In a few places, remove the local 'sequence' variable and replace with
info->sequence - the main reason here being to limit differences between
the various code samples.
--
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
Index: doc/sample1.c
===================================================================
--- doc/sample1.c (revision 1195)
+++ doc/sample1.c (working copy)
@@ -69,7 +69,6 @@
uint8_t buffer[BUFFER_SIZE];
mpeg2dec_t * decoder;
const mpeg2_info_t * info;
- const mpeg2_sequence_t * sequence;
mpeg2_state_t state;
size_t size;
int framenum = 0;
@@ -84,7 +83,6 @@
size = (size_t)-1;
do {
state = mpeg2_parse (decoder);
- sequence = info->sequence;
switch (state) {
case STATE_BUFFER:
size = fread (buffer, 1, BUFFER_SIZE, mpgfile);
@@ -94,8 +92,9 @@
case STATE_END:
case STATE_INVALID_END:
if (info->display_fbuf)
- save_pgm (sequence->width, sequence->height,
- sequence->chroma_width, sequence->chroma_height,
+ save_pgm (info->sequence->width, info->sequence->height,
+ info->sequence->chroma_width,
+ info->sequence->chroma_height,
info->display_fbuf->buf, framenum++);
break;
default:
Index: doc/sample3.c
===================================================================
--- doc/sample3.c (revision 1195)
+++ doc/sample3.c (working copy)
@@ -68,15 +68,12 @@
static void sample3 (FILE * mpgfile)
{
#define BUFFER_SIZE 4096
-#define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
uint8_t buffer[BUFFER_SIZE];
mpeg2dec_t * decoder;
const mpeg2_info_t * info;
- const mpeg2_sequence_t * sequence;
mpeg2_state_t state;
size_t size;
int framenum = 0;
- uint8_t * mbuf[3][3];
uint8_t * fbuf[3][3];
int i, j;
@@ -90,7 +87,6 @@
size = (size_t)-1;
do {
state = mpeg2_parse (decoder);
- sequence = info->sequence;
switch (state) {
case STATE_BUFFER:
size = fread (buffer, 1, BUFFER_SIZE, mpgfile);
@@ -98,18 +94,22 @@
break;
case STATE_SEQUENCE:
for (i = 0; i < 3; i++) {
- mbuf[i][0] = (uint8_t *) malloc (sequence->width *
- sequence->height + 15);
- mbuf[i][1] = (uint8_t *) malloc (sequence->chroma_width *
- sequence->chroma_height + 15);
- mbuf[i][2] = (uint8_t *) malloc (sequence->chroma_width *
- sequence->chroma_height + 15);
- if (!mbuf[i][0] || !mbuf[i][1] || !mbuf[i][2]) {
+ fbuf[i][0] =
+ (uint8_t *) mpeg2_malloc (info->sequence->width *
+ info->sequence->height,
+ MPEG2_ALLOC_YUV);
+ fbuf[i][1] =
+ (uint8_t *) mpeg2_malloc (info->sequence->chroma_width *
+ info->sequence->chroma_height,
+ MPEG2_ALLOC_YUV);
+ fbuf[i][2] =
+ (uint8_t *) mpeg2_malloc (info->sequence->chroma_width *
+ info->sequence->chroma_height,
+ MPEG2_ALLOC_YUV);
+ if (!fbuf[i][0] || !fbuf[i][1] || !fbuf[i][2]) {
fprintf (stderr, "Could not allocate an output buffer.\n");
exit (1);
}
- for (j = 0; j < 3; j++)
- fbuf[i][j] = ALIGN_16 (mbuf[i][j]);
mpeg2_set_buf (decoder, fbuf[i], NULL);
}
break;
@@ -117,13 +117,14 @@
case STATE_END:
case STATE_INVALID_END:
if (info->display_fbuf)
- save_pgm (sequence->width, sequence->height,
- sequence->chroma_width, sequence->chroma_height,
+ save_pgm (info->sequence->width, info->sequence->height,
+ info->sequence->chroma_width,
+ info->sequence->chroma_height,
info->display_fbuf->buf, framenum++);
if (state != STATE_SLICE)
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
- free (mbuf[i][j]);
+ mpeg2_free (fbuf[i][j]);
break;
default:
break;
Index: doc/sample4.c
===================================================================
--- doc/sample4.c (revision 1195)
+++ doc/sample4.c (working copy)
@@ -84,7 +84,9 @@
mpeg2_convert (decoder, mpeg2convert_rgb24, NULL);
pixels = info->sequence->width * info->sequence->height;
for (i = 0; i < 3; i++) {
- fbuf[i][0] = (uint8_t *) malloc (3 * pixels);
+ fbuf[i][0] =
+ (uint8_t *) mpeg2_malloc (3 * pixels,
+ MPEG2_ALLOC_CONVERTED);
fbuf[i][1] = fbuf[i][2] = NULL;
if (!fbuf[i][0]) {
fprintf (stderr, "Could not allocate an output buffer.\n");
@@ -101,7 +103,7 @@
info->display_fbuf->buf[0], framenum++);
if (state != STATE_SLICE)
for (i = 0; i < 3; i++)
- free (fbuf[i][0]);
+ mpeg2_free (fbuf[i][0]);
break;
default:
break;
Index: doc/sample5.c
===================================================================
--- doc/sample5.c (revision 1195)
+++ doc/sample5.c (working copy)
@@ -66,7 +66,6 @@
}
static struct fbuf_s {
- uint8_t * mbuf[3];
uint8_t * yuv[3];
int used;
} fbuf[3];
@@ -87,11 +86,9 @@
static void sample5 (FILE * mpgfile)
{
#define BUFFER_SIZE 4096
-#define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
uint8_t buffer[BUFFER_SIZE];
mpeg2dec_t * decoder;
const mpeg2_info_t * info;
- const mpeg2_sequence_t * sequence;
mpeg2_state_t state;
size_t size;
int framenum = 0;
@@ -108,7 +105,6 @@
size = (size_t)-1;
do {
state = mpeg2_parse (decoder);
- sequence = info->sequence;
switch (state) {
case STATE_BUFFER:
size = fread (buffer, 1, BUFFER_SIZE, mpgfile);
@@ -117,18 +113,22 @@
case STATE_SEQUENCE:
mpeg2_custom_fbuf (decoder, 1);
for (i = 0; i < 3; i++) {
- fbuf[i].mbuf[0] = (uint8_t *) malloc (sequence->width *
- sequence->height + 15);
- fbuf[i].mbuf[1] = (uint8_t *) malloc (sequence->chroma_width *
- sequence->chroma_height + 15);
- fbuf[i].mbuf[2] = (uint8_t *) malloc (sequence->chroma_width *
- sequence->chroma_height + 15);
- if (!fbuf[i].mbuf[0] || !fbuf[i].mbuf[1] || !fbuf[i].mbuf[2]) {
+ fbuf[i].yuv[0] =
+ (uint8_t *) mpeg2_malloc (info->sequence->width *
+ info->sequence->height,
+ MPEG2_ALLOC_YUV);
+ fbuf[i].yuv[1] =
+ (uint8_t *) mpeg2_malloc (info->sequence->chroma_width *
+ info->sequence->chroma_height,
+ MPEG2_ALLOC_YUV);
+ fbuf[i].yuv[2] =
+ (uint8_t *) mpeg2_malloc (info->sequence->chroma_width *
+ info->sequence->chroma_height,
+ MPEG2_ALLOC_YUV);
+ if (!fbuf[i].yuv[0] || !fbuf[i].yuv[1] || !fbuf[i].yuv[2]) {
fprintf (stderr, "Could not allocate an output buffer.\n");
exit (1);
}
- for (j = 0; j < 3; j++)
- fbuf[i].yuv[j] = ALIGN_16 (fbuf[i].mbuf[j]);
fbuf[i].used = 0;
}
for (i = 0; i < 2; i++) {
@@ -144,15 +144,16 @@
case STATE_END:
case STATE_INVALID_END:
if (info->display_fbuf)
- save_pgm (sequence->width, sequence->height,
- sequence->chroma_width, sequence->chroma_height,
+ save_pgm (info->sequence->width, info->sequence->height,
+ info->sequence->chroma_width,
+ info->sequence->chroma_height,
info->display_fbuf->buf, framenum++);
if (info->discard_fbuf)
((struct fbuf_s *)info->discard_fbuf->id)->used = 0;
if (state != STATE_SLICE)
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
- free (fbuf[i].mbuf[j]);
+ mpeg2_free (fbuf[i].yuv[j]);
break;
default:
break;
Index: doc/sample6.c
===================================================================
--- doc/sample6.c (revision 1195)
+++ doc/sample6.c (working copy)
@@ -103,7 +103,9 @@
mpeg2_custom_fbuf (decoder, 1);
pixels = info->sequence->width * info->sequence->height;
for (i = 0; i < 3; i++) {
- fbuf[i].rgb[0] = (uint8_t *) malloc (3 * pixels);
+ fbuf[i].rgb[0] =
+ (uint8_t *) mpeg2_malloc (3 * pixels,
+ MPEG2_ALLOC_CONVERTED);
fbuf[i].rgb[1] = fbuf[i].rgb[2] = NULL;
if (!fbuf[i].rgb[0]) {
fprintf (stderr, "Could not allocate an output buffer.\n");
@@ -130,7 +132,7 @@
((struct fbuf_s *)info->discard_fbuf->id)->used = 0;
if (state != STATE_SLICE)
for (i = 0; i < 3; i++)
- free (fbuf[i].rgb[0]);
+ mpeg2_free (fbuf[i].rgb[0]);
break;
default:
break;
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Libmpeg2-devel mailing list
Libmpeg2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libmpeg2-devel