Fix oggplay-dump-first-frame
[liboggplay.git] / src / tools / oggplay-dump-first-frame.c
blob4223ea7e38d8d8486306b077264c15493b5ea157
1 #include "config.h"
3 #include <oggplay/oggplay.h>
4 #include <stdio.h>
5 #include <stdlib.h>
7 #include <string.h>
9 #include <X11/Xlib.h>
10 #include <Imlib2.h>
12 static int n_frames = 0;
14 void
15 write_png_file(char *fname, OggPlayRGBChannels *data) {
17 Imlib_Image *image;
19 FILE *f = fopen("out.rgb", "wb");
20 fwrite(data->ptro, data->rgb_width * data->rgb_height, 4, f);
21 fclose(f);
24 image = imlib_create_image_using_data(data->rgb_width, data->rgb_height,
25 (unsigned int *)data->ptro);
26 imlib_context_set_image(image);
27 imlib_image_set_format("png");
28 imlib_save_image(fname);
29 imlib_free_image_and_decache();
32 void
33 dump_video_data (OggPlay * player, int track_num, OggPlayVideoData * video_data,
34 int frame) {
36 OggPlayYUVChannels from;
37 OggPlayRGBChannels to;
39 from.ptry = video_data->y;
40 from.ptru = video_data->u;
41 from.ptrv = video_data->v;
42 oggplay_get_video_y_size(player, track_num, &(from.y_width),
43 &(from.y_height));
44 oggplay_get_video_uv_size(player, track_num, &(from.uv_width),
45 &(from.uv_height));
47 FILE *f = fopen("out.y", "wb");
48 fwrite(from.ptry, from.y_width * from.y_height, 1, f);
49 fclose(f);
51 printf("size: %dx%d %dx%d\n", from.y_width, from.y_height, from.uv_width,
52 from.uv_height);
54 to.ptro = malloc(from.y_width * from.y_height * 4);
55 to.rgb_width = from.y_width;
56 to.rgb_height = from.y_height;
58 oggplay_yuv2bgra (&from, &to);
59 printf("now %dx%d\n", to.rgb_width, to.rgb_height);
61 write_png_file("out.png", &to);
62 free(to.ptro);
65 int
66 dump_streams_callback (OggPlay *player, int num_tracks,
67 OggPlayCallbackInfo **track_info, void *user) {
69 int i;
70 //int j;
71 OggPlayDataHeader ** headers;
72 OggPlayVideoData * video_data;
73 //OggPlayAudioData * audio_data;
74 //int required;
75 OggPlayDataType type;
77 for (i = 0; i < num_tracks; i++) {
78 type = oggplay_callback_info_get_type(track_info[i]);
79 headers = oggplay_callback_info_get_headers(track_info[i]);
81 switch (type) {
82 case OGGPLAY_INACTIVE:
83 break;
84 case OGGPLAY_YUV_VIDEO:
86 * there should only be one record
88 if (oggplay_callback_info_get_required(track_info[i]) < 1) {
89 printf("oops\n");
90 break;
92 video_data = oggplay_callback_info_get_video_data(headers[0]);
93 dump_video_data(player, i, video_data, n_frames);
94 exit(0);
95 break;
96 default:
97 break;
101 n_frames++;
103 return 0;
107 main (int argc, char * argv[]) {
109 OggPlay * player;
110 OggPlayReader * reader;
111 int i;
113 if (argc < 2) {
114 printf ("please provide a filename\n");
115 exit (1);
118 if (strlen(argv[1]) > 7 && (strncmp(argv[1], "http://", 7) == 0)) {
119 reader = oggplay_tcp_reader_new(argv[1], NULL, 80);
120 } else {
121 reader = oggplay_file_reader_new(argv[1]);
124 player = oggplay_open_with_reader(reader);
126 if (player == NULL) {
127 printf ("could not initialise oggplay with this file\n");
128 exit (1);
131 for (i = 0; i < oggplay_get_num_tracks (player); i++) {
132 if (oggplay_get_track_type (player, i) == OGGZ_CONTENT_THEORA) {
133 oggplay_set_callback_num_frames (player, i, 1);
135 oggplay_set_track_active(player, i);
138 oggplay_set_data_callback(player, dump_streams_callback, NULL);
139 oggplay_start_decoding(player);
141 oggplay_close (player);
143 return 0;