Added files rejected by import
[melted] / src / modules / core / producer_ppm.c
1 /*
2 * producer_ppm.c -- simple ppm test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "producer_ppm.h"
22 #include <framework/mlt_frame.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
27 static void producer_close( mlt_producer parent );
28
29 mlt_producer producer_ppm_init( void *command )
30 {
31 producer_ppm this = calloc( sizeof( struct producer_ppm_s ), 1 );
32 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
33 {
34 mlt_producer producer = &this->parent;
35
36 producer->get_frame = producer_get_frame;
37 producer->close = producer_close;
38
39 if ( command == NULL )
40 this->command = strdup( "image2raw -n -a -r 3 -ppm /usr/share/pixmaps/*.png" );
41 else
42 this->command = strdup( command );
43
44 this->pipe = popen( this->command, "r" );
45
46 return producer;
47 }
48 free( this );
49 return NULL;
50 }
51
52 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
53 {
54 // Get the frames properties
55 mlt_properties properties = mlt_frame_properties( this );
56
57 // Get the RGB image
58 uint8_t *rgb = mlt_properties_get_data( properties, "image", NULL );
59
60 // Get width and height
61 *width = mlt_properties_get_int( properties, "width" );
62 *height = mlt_properties_get_int( properties, "height" );
63
64 // Convert to requested format
65 if ( *format == mlt_image_yuv422 )
66 {
67 uint8_t *image = malloc( *width * *height * 2 );
68 mlt_convert_rgb24_to_yuv422( rgb, *width, *height, *width * 3, image );
69 mlt_properties_set_data( properties, "image", image, *width * *height * 2, free, NULL );
70 *buffer = image;
71 }
72 else if ( *format == mlt_image_rgb24 )
73 {
74 *buffer = rgb;
75 }
76
77 return 0;
78 }
79
80 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
81 {
82 producer_ppm this = producer->child;
83 int width;
84 int height;
85
86 // Construct a test frame
87 *frame = mlt_frame_init( );
88
89 // Scan the header
90 if ( fscanf( this->pipe, "P6\n%d %d\n255\n", &width, &height ) == 2 )
91 {
92 // Get the frames properties
93 mlt_properties properties = mlt_frame_properties( *frame );
94
95 // Allocate an image
96 uint8_t *image = malloc( width * height * 3 );
97
98 // Read it
99 fread( image, width * height * 3, 1, this->pipe );
100
101 // Pass the data on the frame properties
102 mlt_properties_set_data( properties, "image", image, width * height * 3, free, NULL );
103 mlt_properties_set_int( properties, "width", width );
104 mlt_properties_set_int( properties, "height", height );
105
106 // Push the image callback
107 mlt_frame_push_get_image( *frame, producer_get_image );
108 }
109
110 // Update timecode on the frame we're creating
111 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
112
113 // Calculate the next timecode
114 mlt_producer_prepare_next( producer );
115
116 return 0;
117 }
118
119 static void producer_close( mlt_producer parent )
120 {
121 producer_ppm this = parent->child;
122 pclose( this->pipe );
123 free( this->command );
124 parent->close = NULL;
125 mlt_producer_close( parent );
126 free( this );
127 }
128