repair flickering video encode, bug created last checkin
[goodguy/history.git] / cinelerra-5.0 / mpeg2enc / readpic.c
1 /* readpic.c, read source pictures                                          */
2
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
5 /*
6  * Disclaimer of Warranty
7  *
8  * These software programs are available to the user without any license fee or
9  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10  * any and all warranties, whether express, implied, or statuary, including any
11  * implied warranties or merchantability or of fitness for a particular
12  * purpose.  In no event shall the copyright-holder be liable for any
13  * incidental, punitive, or consequential damages of any kind whatsoever
14  * arising from the use of these programs.
15  *
16  * This disclaimer of warranty extends to the user of these programs and user's
17  * customers, employees, agents, transferees, successors, and assigns.
18  *
19  * The MPEG Software Simulation Group does not represent or warrant that the
20  * programs furnished hereunder are free of infringement of any third-party
21  * patents.
22  *
23  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24  * are subject to royalty fees to patent holders.  Many of these patents are
25  * general enough such that they are unavoidable regardless of implementation
26  * design.
27  *
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "colormodels.h"
34 #include "config.h"
35 #include "global.h"
36
37
38 static void read_quicktime(frame, number)
39 unsigned char *frame[];
40 long number;
41 {
42         int i, j;
43         int r, g, b;
44         int y, u, v;
45         double cr, cg, cb, cu, cv;
46         char name[128];
47         unsigned char *yp, *up, *vp;
48         static unsigned char *u444, *v444, *u422, *v422;
49         static double coef[7][3] = {
50                 {0.2125,0.7154,0.0721}, /* ITU-R Rec. 709 (1990) */
51                 {0.299, 0.587, 0.114},  /* unspecified */
52                 {0.299, 0.587, 0.114},  /* reserved */
53                 {0.30,  0.59,  0.11},   /* FCC */
54                 {0.299, 0.587, 0.114},  /* ITU-R Rec. 624-4 System B, G */
55                 {0.299, 0.587, 0.114},  /* SMPTE 170M */
56                 {0.212, 0.701, 0.087}}; /* SMPTE 240M (1987) */
57         static long rtoy_tab[256], gtoy_tab[256], btoy_tab[256];
58         static long rtou_tab[256], gtou_tab[256], btou_tab[256];
59         static long rtov_tab[256], gtov_tab[256], btov_tab[256];
60         static int need_tables = 1;   // Initialize tables on first read
61         int colormodel;
62         long real_number;
63
64         i = matrix_coefficients;
65         if(i > 8) i = 3;
66
67         cr = coef[i - 1][0];
68         cg = coef[i - 1][1];
69         cb = coef[i - 1][2];
70         cu = 0.5 / (1.0 - cb);
71         cv = 0.5 / (1.0 - cr);
72
73 // Allocate output buffers
74         if(chroma_format == CHROMA444)
75         {
76 // Not supported by libMPEG3
77         u444 = frame[1];
78         v444 = frame[2];
79         }
80         else
81         {
82         if (!u444)
83         {
84                 if (!(u444 = (unsigned char *)malloc(width*height)))
85                         error("malloc failed");
86                 if (!(v444 = (unsigned char *)malloc(width*height)))
87                         error("malloc failed");
88                 if (chroma_format==CHROMA420)
89                 {
90                         if (!(u422 = (unsigned char *)malloc((width>>1)*height)))
91                                 error("malloc failed");
92                         if (!(v422 = (unsigned char *)malloc((width>>1)*height)))
93                                 error("malloc failed");
94                 }
95         }
96         }
97
98 // Initialize YUV tables
99         if(need_tables)
100         {
101                 for(i = 0; i < 256; i++)
102                 {
103                         rtoy_tab[i] = (long)( 0.2990 * 65536 * i);
104                         rtou_tab[i] = (long)(-0.1687 * 65536 * i);
105                         rtov_tab[i] = (long)( 0.5000 * 65536 * i);
106
107                         gtoy_tab[i] = (long)( 0.5870 * 65536 * i);
108                         gtou_tab[i] = (long)(-0.3320 * 65536 * i);
109                         gtov_tab[i] = (long)(-0.4187 * 65536 * i);
110
111                         btoy_tab[i] = (long)( 0.1140 * 65536 * i);
112                         btou_tab[i] = (long)( 0.5000 * 65536 * i);
113                         btov_tab[i] = (long)(-0.0813 * 65536 * i);
114                 }
115                 need_tables = 0;
116         }
117
118         real_number = (long)((double)quicktime_frame_rate(qt_file, 0) / 
119                         frame_rate * 
120                         number + 
121                         0.5);
122         quicktime_set_video_position(qt_file, 
123                 real_number, 
124                 0);
125
126 //printf("readframe 1 %d %d\n", width, height);
127         quicktime_set_row_span(qt_file, width);
128         quicktime_set_window(qt_file,
129                 0, 
130                 0,
131                 horizontal_size,
132                 vertical_size,
133                 horizontal_size,
134                 vertical_size);
135         quicktime_set_cmodel(qt_file, (chroma_format == 1) ? BC_YUV420P : BC_YUV422P);
136         
137         quicktime_decode_video(qt_file, 
138                 frame, 
139                 0);
140 //printf("readframe 2\n");
141 }
142
143 static void read_mpeg(long number, unsigned char *frame[])
144 {
145         int i;
146         char name[128];
147         long real_number;
148
149 // Normalize frame_rate
150         real_number = (long)((double)mpeg3_frame_rate(mpeg_file, 0) / 
151                 frame_rate * 
152                 number + 
153                 0.5);
154
155         while(mpeg3_get_frame(mpeg_file, 0) <= real_number)
156                 mpeg3_read_yuvframe(mpeg_file,
157                         frame[0],
158                         frame[1],
159                         frame[2],
160                         0,
161                         0,
162                         horizontal_size,
163                         vertical_size,
164                         0);
165 /* Terminate encoding after processing this frame */
166         if(mpeg3_end_of_video(mpeg_file, 0)) frames_scaled = 1; 
167 }
168
169 static void read_stdin(long number, unsigned char *frame[])
170 {
171         int chroma_denominator = 1;
172         unsigned char data[5];
173
174
175         if(chroma_format == 1) chroma_denominator = 2;
176
177         fread(data, 4, 1, stdin_fd);
178
179 // Terminate encoding before processing this frame
180         if(data[0] == 0xff && data[1] == 0xff && data[2] == 0xff && data[3] == 0xff)
181         {
182                 frames_scaled = 0;
183                 return;
184         }
185
186         fread(frame[0], width * height, 1, stdin_fd);
187         fread(frame[1], width / 2 * height / chroma_denominator, 1, stdin_fd);
188         fread(frame[2], width / 2 * height / chroma_denominator, 1, stdin_fd);
189 }
190
191 static void read_buffers(long number, unsigned char *frame[])
192 {
193         int chroma_denominator = 1;
194         unsigned char data[5];
195
196         if(chroma_format == 1) chroma_denominator = 2;
197
198         pthread_mutex_lock(&input_lock);
199
200         if(input_buffer_end) 
201         {
202                 frames_scaled = 0;
203                 pthread_mutex_unlock(&copy_lock);
204                 pthread_mutex_unlock(&output_lock);
205                 return;
206         }
207
208         memcpy(frame[0], input_buffer_y, width * height);
209         memcpy(frame[1], input_buffer_u, width / 2 * height / chroma_denominator);
210         memcpy(frame[2], input_buffer_v, width / 2 * height / chroma_denominator);
211         pthread_mutex_unlock(&copy_lock);
212         pthread_mutex_unlock(&output_lock);
213 }
214
215 void readframe(int frame_num, uint8_t *frame[])
216 {
217         int n;
218         n = frame_num % (2 * READ_LOOK_AHEAD);
219
220         frame[0] = frame_buffers[n][0];
221         frame[1] = frame_buffers[n][1];
222         frame[2] = frame_buffers[n][2];
223         
224
225         switch (inputtype)
226         {
227                 case T_QUICKTIME:
228                         read_quicktime(frame, frame_num);
229                         break;
230                 case T_MPEG:
231                         read_mpeg(frame_num, frame);
232                         break;
233                 case T_STDIN:
234                         read_stdin(frame_num, frame);
235                         break;
236                 case T_BUFFERS:
237                         read_buffers(frame_num, frame);
238                         break;
239                 default:
240                         break;
241   }
242 }