r560: reverse interlace -yuvmpeg patch
[cinelerra_cv/mob.git] / mpeg2enc / readpic.c
bloba600f1cca6de4b217b4c3686dda869352923dce4
1 /* readpic.c, read source pictures */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
6 * Disclaimer of Warranty
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.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
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.
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.
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "colormodels.h"
33 #include "config.h"
34 #include "global.h"
37 static void read_quicktime(frame, number)
38 unsigned char *frame[];
39 long number;
41 int i, j;
42 int r, g, b;
43 int y, u, v;
44 double cr, cg, cb, cu, cv;
45 char name[128];
46 unsigned char *yp, *up, *vp;
47 static unsigned char *u444, *v444, *u422, *v422;
48 static double coef[7][3] = {
49 {0.2125,0.7154,0.0721}, /* ITU-R Rec. 709 (1990) */
50 {0.299, 0.587, 0.114}, /* unspecified */
51 {0.299, 0.587, 0.114}, /* reserved */
52 {0.30, 0.59, 0.11}, /* FCC */
53 {0.299, 0.587, 0.114}, /* ITU-R Rec. 624-4 System B, G */
54 {0.299, 0.587, 0.114}, /* SMPTE 170M */
55 {0.212, 0.701, 0.087}}; /* SMPTE 240M (1987) */
56 static long rtoy_tab[256], gtoy_tab[256], btoy_tab[256];
57 static long rtou_tab[256], gtou_tab[256], btou_tab[256];
58 static long rtov_tab[256], gtov_tab[256], btov_tab[256];
59 static int need_tables = 1; // Initialize tables on first read
60 int colormodel;
61 long real_number;
63 i = matrix_coefficients;
64 if(i > 8) i = 3;
66 cr = coef[i - 1][0];
67 cg = coef[i - 1][1];
68 cb = coef[i - 1][2];
69 cu = 0.5 / (1.0 - cb);
70 cv = 0.5 / (1.0 - cr);
72 // Allocate output buffers
73 if(chroma_format == CHROMA444)
75 // Not supported by libMPEG3
76 u444 = frame[1];
77 v444 = frame[2];
79 else
81 if (!u444)
83 if (!(u444 = (unsigned char *)malloc(width*height)))
84 error("malloc failed");
85 if (!(v444 = (unsigned char *)malloc(width*height)))
86 error("malloc failed");
87 if (chroma_format==CHROMA420)
89 if (!(u422 = (unsigned char *)malloc((width>>1)*height)))
90 error("malloc failed");
91 if (!(v422 = (unsigned char *)malloc((width>>1)*height)))
92 error("malloc failed");
97 // Initialize YUV tables
98 if(need_tables)
100 for(i = 0; i < 256; i++)
102 rtoy_tab[i] = (long)( 0.2990 * 65536 * i);
103 rtou_tab[i] = (long)(-0.1687 * 65536 * i);
104 rtov_tab[i] = (long)( 0.5000 * 65536 * i);
106 gtoy_tab[i] = (long)( 0.5870 * 65536 * i);
107 gtou_tab[i] = (long)(-0.3320 * 65536 * i);
108 gtov_tab[i] = (long)(-0.4187 * 65536 * i);
110 btoy_tab[i] = (long)( 0.1140 * 65536 * i);
111 btou_tab[i] = (long)( 0.5000 * 65536 * i);
112 btov_tab[i] = (long)(-0.0813 * 65536 * i);
114 need_tables = 0;
117 real_number = (long)((double)quicktime_frame_rate(qt_file, 0) /
118 frame_rate *
119 number +
120 0.5);
121 quicktime_set_video_position(qt_file,
122 real_number,
125 //printf("readframe 1 %d %d\n", width, height);
126 quicktime_set_row_span(qt_file, width);
127 quicktime_set_window(qt_file,
130 horizontal_size,
131 vertical_size,
132 horizontal_size,
133 vertical_size);
134 quicktime_set_cmodel(qt_file, (chroma_format == 1) ? BC_YUV420P : BC_YUV422P);
136 quicktime_decode_video(qt_file,
137 frame,
139 //printf("readframe 2\n");
142 static void read_mpeg(long number, unsigned char *frame[])
144 int i;
145 char name[128];
146 long real_number;
148 // Normalize frame_rate
149 real_number = (long)((double)mpeg3_frame_rate(mpeg_file, 0) /
150 frame_rate *
151 number +
152 0.5);
154 while(mpeg3_get_frame(mpeg_file, 0) <= real_number)
155 mpeg3_read_yuvframe(mpeg_file,
156 frame[0],
157 frame[1],
158 frame[2],
161 horizontal_size,
162 vertical_size,
164 /* Terminate encoding after processing this frame */
165 if(mpeg3_end_of_video(mpeg_file, 0)) frames_scaled = 1;
168 static void read_stdin(long number, unsigned char *frame[])
170 int chroma_denominator = 1;
171 unsigned char data[5];
174 if(chroma_format == 1) chroma_denominator = 2;
176 fread(data, 4, 1, stdin_fd);
178 // Terminate encoding before processing this frame
179 if(data[0] == 0xff && data[1] == 0xff && data[2] == 0xff && data[3] == 0xff)
181 frames_scaled = 0;
182 return;
185 fread(frame[0], width * height, 1, stdin_fd);
186 fread(frame[1], width / 2 * height / chroma_denominator, 1, stdin_fd);
187 fread(frame[2], width / 2 * height / chroma_denominator, 1, stdin_fd);
190 static void read_buffers(long number, unsigned char *frame[])
192 int chroma_denominator = 1;
193 unsigned char data[5];
195 if(chroma_format == 1) chroma_denominator = 2;
197 pthread_mutex_lock(&input_lock);
199 if(input_buffer_end)
201 frames_scaled = 0;
202 pthread_mutex_unlock(&copy_lock);
203 pthread_mutex_unlock(&output_lock);
204 return;
207 memcpy(frame[0], input_buffer_y, width * height);
208 memcpy(frame[1], input_buffer_u, width / 2 * height / chroma_denominator);
209 memcpy(frame[2], input_buffer_v, width / 2 * height / chroma_denominator);
210 pthread_mutex_unlock(&copy_lock);
211 pthread_mutex_unlock(&output_lock);
214 void readframe(int frame_num, uint8_t *frame[])
216 int n;
217 n = frame_num % (2 * READ_LOOK_AHEAD);
219 frame[0] = frame_buffers[n][0];
220 frame[1] = frame_buffers[n][1];
221 frame[2] = frame_buffers[n][2];
224 switch (inputtype)
226 case T_QUICKTIME:
227 read_quicktime(frame, frame_num);
228 break;
229 case T_MPEG:
230 read_mpeg(frame_num, frame);
231 break;
232 case T_STDIN:
233 read_stdin(frame_num, frame);
234 break;
235 case T_BUFFERS:
236 read_buffers(frame_num, frame);
237 break;
238 default:
239 break;