Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / 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 "config.h"
34 #include "global.h"
35
36
37 static void read_mpeg(long number, unsigned char *frame[])
38 {
39         int i;
40         char name[128];
41         long real_number;
42
43 // Normalize frame_rate
44         real_number = (long)((double)mpeg3_frame_rate(mpeg_file, 0) / 
45                 frame_rate * 
46                 number + 
47                 0.5);
48
49         while(mpeg3_get_frame(mpeg_file, 0) <= real_number)
50                 mpeg3_read_yuvframe(mpeg_file,
51                         frame[0],
52                         frame[1],
53                         frame[2],
54                         0,
55                         0,
56                         horizontal_size,
57                         vertical_size,
58                         0);
59 /* Terminate encoding after processing this frame */
60         if(mpeg3_end_of_video(mpeg_file, 0)) frames_scaled = 1; 
61 }
62
63 static void read_stdin(long number, unsigned char *frame[])
64 {
65         int chroma_denominator = 1;
66         unsigned char data[5];
67
68
69         if(chroma_format == 1) chroma_denominator = 2;
70
71         fread(data, 4, 1, stdin_fd);
72
73 // Terminate encoding before processing this frame
74         if(data[0] == 0xff && data[1] == 0xff && data[2] == 0xff && data[3] == 0xff)
75         {
76                 frames_scaled = 0;
77                 return;
78         }
79
80         fread(frame[0], width * height, 1, stdin_fd);
81         fread(frame[1], width / 2 * height / chroma_denominator, 1, stdin_fd);
82         fread(frame[2], width / 2 * height / chroma_denominator, 1, stdin_fd);
83 }
84
85 static void read_buffers(long number, unsigned char *frame[])
86 {
87         int chroma_denominator = 1;
88         unsigned char data[5];
89
90         if(chroma_format == 1) chroma_denominator = 2;
91
92         pthread_mutex_lock(&input_lock);
93
94         if(input_buffer_end) 
95         {
96                 frames_scaled = 0;
97                 pthread_mutex_unlock(&copy_lock);
98                 pthread_mutex_unlock(&output_lock);
99                 return;
100         }
101
102         memcpy(frame[0], input_buffer_y, width * height);
103         memcpy(frame[1], input_buffer_u, width / 2 * height / chroma_denominator);
104         memcpy(frame[2], input_buffer_v, width / 2 * height / chroma_denominator);
105         pthread_mutex_unlock(&copy_lock);
106         pthread_mutex_unlock(&output_lock);
107 }
108
109 void readframe(int frame_num, uint8_t *frame[])
110 {
111         int n;
112         n = frame_num % (2 * READ_LOOK_AHEAD);
113
114         frame[0] = frame_buffers[n][0];
115         frame[1] = frame_buffers[n][1];
116         frame[2] = frame_buffers[n][2];
117         
118
119         switch (inputtype)
120         {
121                 case T_MPEG:
122                         read_mpeg(frame_num, frame);
123                         break;
124                 case T_STDIN:
125                         read_stdin(frame_num, frame);
126                         break;
127                 case T_BUFFERS:
128                         read_buffers(frame_num, frame);
129                         break;
130                 default:
131                         break;
132   }
133 }