forked from cmarrin/videomonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toyuv.c
451 lines (377 loc) · 13.1 KB
/
toyuv.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// libav2yuv
// adapted from
// avcodec_sample.0.4.9.cpp
// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// This version is for the 0.4.9-pre1 release of ffmpeg. This release adds the
// av_read_frame() API call, which simplifies the reading of video frames
// considerably.
//
// gcc -O3 -I/usr/local/include -I/usr/local/include/mjpegtools -lavcodec -lavformat -lavutil -lmjpegutils -lswscale -lmp3lame -lz -ltheora -logg -lvorbis -lx264 -lxvidcore -lfaac -lfaad -lvorbisenc -lbz2 toyuv.c -o toyuv
//
// I really should put history here
// 7th July 2008 - Added Force Format option
// 4th July 2008 - Added Aspect Ratio Constants
// 3rd July 2008 - Will choose the first stream found if no stream is specified
// 24th Feb 2008 - Found an unexpected behaviour where frames were being dropped. libav said that no frame was decoded.
// Have output the previous frame in this instance.
//
#include <yuv4mpeg.h>
#include <mpegconsts.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#define PAL "PAL"
#define PAL_WIDE "PAL_WIDE"
#define NTSC "NTSC"
#define NTSC_WIDE "NTSC_WIDE"
void chromacpy (uint8_t *dst[3], AVFrame *src, y4m_stream_info_t *sinfo)
{
int y,h,w;
int cw,ch;
w = y4m_si_get_plane_width(sinfo,0);
h = y4m_si_get_plane_height(sinfo,0);
cw = y4m_si_get_plane_width(sinfo,1);
ch = y4m_si_get_plane_height(sinfo,1);
for (y=0; y<h; y++) {
memcpy(dst[0]+y*w,(src->data[0])+y*src->linesize[0],w);
if (y<ch) {
memcpy(dst[1]+y*cw,(src->data[1])+y*src->linesize[1],cw);
memcpy(dst[2]+y*cw,(src->data[2])+y*src->linesize[2],cw);
}
}
}
void chromalloc(uint8_t *m[3],y4m_stream_info_t *sinfo)
{
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
m[0] = (uint8_t *)malloc( fs );
m[1] = (uint8_t *)malloc( cfs);
m[2] = (uint8_t *)malloc( cfs);
}
static void print_usage()
{
fprintf (stderr,
"usage: libav2yuv [-s<stream>] [-Ip|b|t] [-F<rate>] [-A<aspect>] [-S<chroma>] [-o<outputfile] <filename>\n"
"converts any media file recognised by libav to yuv4mpeg stream\n"
"\n"
"\t -I<pbt> Force interlace mode overides parameters read from media file\n"
"\t -F<n:d> Force framerate\n"
"\t -f <fmt> Force format type (if incorrectly detected)\n"
"\t -A(<n:d>|PAL|PAL_WIDE|NTSC|NTSC_WIDE) Force aspect ratio\n"
"\t -S<chroma> Force chroma subsampling mode\n"
"\t if the mode in the stream is unsupported will upsample to YUV444\n"
"\t -c Force conversion to chroma mode (requires a chroma mode)\n"
"\t -s select stream other than stream 0\n"
"\t -o<outputfile> write to file rather than stdout\n"
"\t -h print this help\n"
);
}
int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx;
AVInputFormat *avif = NULL;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrame444;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
int fdOut = 1 ;
int yuv_interlacing = Y4M_UNKNOWN;
int yuv_ss_mode = Y4M_UNKNOWN;
y4m_ratio_t yuv_frame_rate;
y4m_ratio_t yuv_aspect;
// need something for chroma subsampling type.
int write_error_code;
int header_written = 0;
int convert = 0;
int stream = 0;
enum PixelFormat convert_mode;
const static char *legal_flags = "chI:F:A:S:o:s:f:";
int y;
int frame_data_size ;
uint8_t *yuv_data[3] ;
y4m_stream_info_t streaminfo;
y4m_frame_info_t frameinfo;
y4m_init_stream_info(&streaminfo);
y4m_init_frame_info(&frameinfo);
yuv_frame_rate.d = 0;
yuv_aspect.d = 0;
// Register all formats and codecs
av_register_all();
while ((i = getopt (argc, argv, legal_flags)) != -1) {
switch (i) {
case 'I':
switch (optarg[0]) {
case 'p': yuv_interlacing = Y4M_ILACE_NONE; break;
case 't': yuv_interlacing = Y4M_ILACE_TOP_FIRST; break;
case 'b': yuv_interlacing = Y4M_ILACE_BOTTOM_FIRST; break;
default:
mjpeg_error("Unknown value for interlace: '%c'", optarg[0]);
return -1;
break;
}
break;
case 'F':
if( Y4M_OK != y4m_parse_ratio(&yuv_frame_rate, optarg) )
mjpeg_error_exit1 ("Syntax for frame rate should be Numerator:Denominator");
break;
case 'A':
if( Y4M_OK != y4m_parse_ratio(&yuv_aspect, optarg) ) {
if (!strcmp(optarg,PAL)) {
y4m_parse_ratio(&yuv_aspect, "128:117");
} else if (!strcmp(optarg,PAL_WIDE)) {
y4m_parse_ratio(&yuv_aspect, "640:351");
} else if (!strcmp(optarg,NTSC)) {
y4m_parse_ratio(&yuv_aspect, "4320:4739");
} else if (!strcmp(optarg,NTSC_WIDE)) {
y4m_parse_ratio(&yuv_aspect, "5760:4739");
} else {
mjpeg_error_exit1 ("Syntax for aspect ratio should be Numerator:Denominator");
}
}
break;
case 'S':
yuv_ss_mode = y4m_chroma_parse_keyword(optarg);
if (yuv_ss_mode == Y4M_UNKNOWN) {
mjpeg_error("Unknown subsampling mode option: %s", optarg);
mjpeg_error("Try: 420mpeg2 444 422 411");
return -1;
}
break;
case 'o':
fdOut = open (optarg,O_CREAT|O_WRONLY,0644);
if (fdOut == -1) {
mjpeg_error_exit1 ("Cannot open file for writing");
}
break;
case 'c':
convert = 1;
break;
case 's':
stream = atoi(optarg);
break;
case 'f':
avif = av_find_input_format (optarg);
break;
case 'h':
case '?':
print_usage (argv);
return 0 ;
break;
}
}
//fprintf (stderr,"optind: %d\n",optind);
optind--;
argc -= optind;
argv += optind;
if (argc == 1) {
print_usage (argv);
return 0 ;
}
// Open video file
if(av_open_input_file(&pFormatCtx, argv[1], avif, 0, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);
// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
// mark debug
//fprintf (stderr,"Video Codec ID: %d (%s)\n",pFormatCtx->streams[i]->codec->codec_id ,pFormatCtx->streams[i]->codec->codec_name);
if (videoStream == -1 && stream == 0) {
// May still be overridden by the -s option
videoStream=i;
}
if (stream == i) {
videoStream=i;
break;
}
}
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
return -1; // Codec not found
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1; // Could not open codec
// Read framerate, aspect ratio and chroma subsampling from Codec
if (yuv_frame_rate.d == 0) {
yuv_frame_rate.n = pFormatCtx->streams[videoStream]->r_frame_rate.num;
yuv_frame_rate.d = pFormatCtx->streams[videoStream]->r_frame_rate.den;
}
if (yuv_aspect.d == 0) {
yuv_aspect.n = pCodecCtx-> sample_aspect_ratio.num;
yuv_aspect.d = pCodecCtx-> sample_aspect_ratio.den;
}
// 0:0 is an invalid aspect ratio default to 1:1
if (yuv_aspect.d == 0 || yuv_aspect.n == 0 ) {
yuv_aspect.n=1;
yuv_aspect.d=1;
}
if (convert) {
if (yuv_ss_mode == Y4M_UNKNOWN) {
print_usage();
return 0;
} else {
y4m_accept_extensions(1);
switch (yuv_ss_mode) {
case Y4M_CHROMA_420MPEG2: convert_mode = PIX_FMT_YUV420P; break;
case Y4M_CHROMA_422: convert_mode = PIX_FMT_YUV422P; break;
case Y4M_CHROMA_444: convert_mode = PIX_FMT_YUV444P; break;
case Y4M_CHROMA_411: convert_mode = PIX_FMT_YUV411P; break;
case Y4M_CHROMA_420JPEG: convert_mode = PIX_FMT_YUVJ420P; break;
default:
mjpeg_error_exit1("Cannot convert to this chroma mode");
break;
}
}
} else if (yuv_ss_mode == Y4M_UNKNOWN) {
switch (pCodecCtx->pix_fmt) {
case PIX_FMT_YUV420P: yuv_ss_mode=Y4M_CHROMA_420MPEG2; break;
case PIX_FMT_YUV422P: yuv_ss_mode=Y4M_CHROMA_422; break;
case PIX_FMT_YUV444P: yuv_ss_mode=Y4M_CHROMA_444; break;
case PIX_FMT_YUV411P: yuv_ss_mode=Y4M_CHROMA_411; break;
case PIX_FMT_YUVJ420P: yuv_ss_mode=Y4M_CHROMA_420JPEG; break;
default:
yuv_ss_mode=Y4M_CHROMA_444;
convert_mode = PIX_FMT_YUV444P;
// is there a warning function
mjpeg_error("Unsupported Chroma mode. Upsampling to YUV444\n");
// enable advanced yuv stream
y4m_accept_extensions(1);
convert = 1;
break;
}
}
// Allocate video frame
pFrame=avcodec_alloc_frame();
// Output YUV format details
// is there some mjpeg_info functions?
fprintf (stderr,"YUV Aspect Ratio: %d:%d\n",yuv_aspect.n,yuv_aspect.d);
fprintf (stderr,"YUV frame rate: %d:%d\n",yuv_frame_rate.n,yuv_frame_rate.d);
fprintf (stderr,"YUV Chroma Subsampling: %d\n",yuv_ss_mode);
// Set the YUV stream details
// Interlace is handled when the first frame is read.
y4m_si_set_sampleaspect(&streaminfo, yuv_aspect);
y4m_si_set_framerate(&streaminfo, yuv_frame_rate);
y4m_si_set_chroma(&streaminfo, yuv_ss_mode);
// Loop until nothing read
while(av_read_frame(pFormatCtx, &packet)>=0)
{
// Is this a packet from the video stream?
if(packet.stream_index==videoStream)
{
// Decode video frame
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
packet.data, packet.size);
// Did we get a video frame?
if(frameFinished)
{
// Save the frame to disk
// As we don't know interlacing until the first frame
// we wait until the first frame is read before setting the interlace flag
// and outputting the YUV header
// It also appears that some codecs don't set width or height until the first frame either
if (!header_written) {
if (yuv_interlacing == Y4M_UNKNOWN) {
if (pFrame->interlaced_frame) {
if (pFrame->top_field_first) {
yuv_interlacing = Y4M_ILACE_TOP_FIRST;
} else {
yuv_interlacing = Y4M_ILACE_BOTTOM_FIRST;
}
} else {
yuv_interlacing = Y4M_ILACE_NONE;
}
}
if (convert) {
// initialise conversion to different chroma subsampling
pFrame444=avcodec_alloc_frame();
numBytes=avpicture_get_size(convert_mode, pCodecCtx->width, pCodecCtx->height);
buffer=(uint8_t *)malloc(numBytes);
avpicture_fill((AVPicture *)pFrame444, buffer, convert_mode, pCodecCtx->width, pCodecCtx->height);
}
y4m_si_set_interlace(&streaminfo, yuv_interlacing);
y4m_si_set_width(&streaminfo, pCodecCtx->width);
y4m_si_set_height(&streaminfo, pCodecCtx->height);
chromalloc(yuv_data,&streaminfo);
fprintf (stderr,"YUV interlace: %d\n",yuv_interlacing);
fprintf (stderr,"YUV Output Resolution: %dx%d\n",pCodecCtx->width, pCodecCtx->height);
if ((write_error_code = y4m_write_stream_header(fdOut, &streaminfo)) != Y4M_OK)
{
mjpeg_error("Write header failed: %s", y4m_strerr(write_error_code));
}
header_written = 1;
}
if (convert) {
// convert to 444
/*
+#ifdef HAVE_LIBSWSCALE
+ struct SwsContext* img_convert_ctx =
+ sws_getContext(context->width, context->height, PIX_FMT_RGB24,
+ context->width, context->height, context->pix_fmt,
+ SWS_BICUBIC, NULL, NULL, NULL);
+
+ sws_scale(img_convert_ctx, pict->data, pict->linesize,
+ 0, context->height, encodable->data,
+ encodable->linesize);
+
+ sws_freeContext (img_convert_ctx);
+#else
img_convert((AVPicture *)encodable, context->pix_fmt, (AVPicture *)pict, PIX_FMT_RGB24, context->width, context->height);
-
+ (AVPicture *)pict, PIX_FMT_RGB24,
+ context->width, context->height);
+#endif
*/
struct SwsContext* img_convert_ctx =
sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,
0, pCodecCtx->height, pFrame444->data,
pFrame444->linesize);
sws_freeContext (img_convert_ctx);
//img_convert((AVPicture *)pFrame444, convert_mode, (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
chromacpy(yuv_data,pFrame444,&streaminfo);
} else {
chromacpy(yuv_data,pFrame,&streaminfo);
}
write_error_code = y4m_write_frame( fdOut, &streaminfo, &frameinfo, yuv_data);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
y4m_fini_stream_info(&streaminfo);
y4m_fini_frame_info(&frameinfo);
free(yuv_data[0]);
free(yuv_data[1]);
free(yuv_data[2]);
// Free the YUV frame
av_free(pFrame);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
av_close_input_file(pFormatCtx);
return 0;
}