File: rechunk.c

package info (click to toggle)
libquicktime 2%3A1.2.4-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,832 kB
  • sloc: ansic: 55,312; sh: 10,976; makefile: 473; sed: 16
file content (260 lines) | stat: -rw-r--r-- 6,547 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*******************************************************************************
 rechunk.c

 libquicktime - A library for reading and writing quicktime/avi/mp4 files.
 http://libquicktime.sourceforge.net

 Copyright (C) 2002 Heroine Virtual Ltd.
 Copyright (C) 2002-2011 Members of the libquicktime project.

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.

 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.

 You should have received a copy of the GNU Lesser General Public License along
 with this library; if not, write to the Free Software Foundation, Inc., 51
 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************/ 

#include "lqt_private.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

static int usage(void)
{
	printf("usage: qtrechunk [-f framerate] [-w width] [-h height] [-c fourcc] [-b bitdepth] [-l file_list | <input frames>] <output movie>\n");
	printf("	Concatenate input frames into a Quicktime movie.\n\n");
	printf("	-l <file_list> reads filenames of the frame from\n");
        printf("           <file_list>, which must contain one filename per line\n");
	printf("        -f specifies the framerate either as float or rational number\n");
        printf("           e.g. -f 30000:1001\n");
	exit(1);
	return 0;
}

static char ** add_frames_from_file(char ** input_frames,
                             int * total_input_frames, char * list_filename)
  {
  FILE * input;
  char * pos;
#ifdef PATH_MAX
  char filename[PATH_MAX+10];
#else
  char *filename = NULL;
#endif

  input = fopen(list_filename, "r");
  if(!input)
    {
    fprintf(stderr, "Cannot open %s: %s\n", list_filename, strerror(errno));
    exit(1);
    return (char**)0;
    }

#ifdef PATH_MAX
  while(fgets(filename, PATH_MAX+10, input))
#else
  while(getline(&filename, NULL, input) != -1)
#endif
    {
    /* Delete trailing '\n' and '\r' */

    pos = &(filename[strlen(filename)-1]);

    while(1)
      {
      if(*pos == '\r')
        *pos = '\0';
      if(*pos == '\n')
        *pos = '\0';
      else
        break;

      if(pos == filename)
        {
#ifndef PATH_MAX
        free(filename);
#endif
        return input_frames;
        }
      
      pos--;      
      }

    /* Add filename */

    (*total_input_frames)++;
    input_frames = realloc(input_frames, sizeof(char*) * *total_input_frames);
    input_frames[*total_input_frames - 1] = strdup(filename);
    //    fprintf(stderr, "Adding file %s\n", input_frames[*total_input_frames - 1]);
#ifndef PATH_MAX
    free(filename);
    filename = NULL;
#endif
    }
  return input_frames;
  }

int main(int argc, char *argv[])
{
        lqt_codec_info_t ** codec_info;
	quicktime_t *file;
	FILE *input;
	int i, j;
	char *output = 0;
	uint8_t *data = 0;
	int bytes = 0;
	float output_rate = 0;
	char **input_frames = 0;
	int total_input_frames = 0;
	int width = 720, height = 480, bit_depth = 24;
	char compressor[5] = "yv12";
        int output_rate_num = 0;
	int output_rate_den = 0;
	
	if(argc < 3)
	{
		usage();
	}

	for(i = 1, j = 0; i < argc; i++)
	{
		if(!strcmp(argv[i], "-f"))
		{
			if(i + 1 < argc)
			{
                                i++;
				if(sscanf(argv[i], "%d:%d", &output_rate_num,
                                          &output_rate_den) < 2)
                                  {
                                  output_rate = atof(argv[i]);
                                  output_rate_num = 0;
                                  }
                                fprintf(stderr, "Rate: %f (%d:%d)\n", output_rate,
                                        output_rate_num, output_rate_den);
                        }
			else
				usage();
		}
		else
		if(!strcmp(argv[i], "-w"))
		{
			if(i + 1 < argc)
				width = atol(argv[++i]);
			else
				usage();
		}
		else
		if(!strcmp(argv[i], "-h"))
		{
			if(i + 1 < argc)
				height = atol(argv[++i]);
			else
				usage();
		}
		else
		if(!strcmp(argv[i], "-c"))
		{
			if(i + 1 < argc)
			{
				strncpy(compressor, argv[++i], 4);
			}
			else
				usage();
		}
		else
		if(!strcmp(argv[i], "-b"))
		{
			if(i + 1 < argc)
				bit_depth = atoi(argv[++i]);
			else
				usage();
		}
		else if(i == argc - 1)
		{
			output = argv[i];
		}
		else
		if(!strcmp(argv[i], "-l"))
		{
			if(i + 1 < argc)
                                input_frames =
                                  add_frames_from_file(input_frames,
                                                       &total_input_frames, argv[++i]);
			else
				usage();
		}
		else
		{
			total_input_frames++;
			input_frames = realloc(input_frames, sizeof(char*) * total_input_frames);
			input_frames[total_input_frames - 1] = argv[i];
		}
	}

	input = fopen(output, "rb");
	if(input != NULL)
	{
		printf("Output file already exists.\n");
		exit(1);
	}

	if(!(file = quicktime_open(output, 0, 1)))
	{
		printf("Open failed\n");
		exit(1);
	}
	if(!output_rate_num)
          quicktime_set_video(file, 1, width, height, output_rate, compressor);
        else
	  {
          codec_info = lqt_find_video_codec(compressor, 1);
	  lqt_add_video_track(file, width, height, output_rate_den, output_rate_num, 
			      *codec_info);
          lqt_destroy_codec_info(codec_info);
          }
	quicktime_set_depth(file, bit_depth, 0);
	for(i = 0; i < total_input_frames; i++)
	{
/* Get output file */
		if(!(input = fopen(input_frames[i], "rb")))
		{
			perror("Open failed");
			continue;
		}

/* Get input frame */
		fseek(input, 0, SEEK_END);
		bytes = ftell(input);
		fseek(input, 0, SEEK_SET);
		data = realloc(data, bytes);

		fread(data, bytes, 1, input);
		quicktime_write_frame(file, data, bytes, 0);
		fclose(input);

                if(!(i % 10))
                  {
                  if(i)
                    {
                    for(j = 0; j < 17; j++)
                      putchar(0x08);
                    }
                  printf("%6.2f%% Completed", 100.0 * (float)(i+1) / (float)(total_input_frames));
                  fflush(stdout);
                  }
        }
        printf("\n");
	quicktime_close(file);

	return 0;
}