2 * Copyright © 2010 Mozilla Foundation
4 * This program is made available under an ISC-style license. See the
5 * accompanying file LICENSE for details.
12 #include "nestegg/nestegg.h"
18 stdio_read(void * p
, size_t length
, void * fp
)
22 r
= fread(p
, length
, 1, fp
);
23 if (r
== 0 && feof(fp
))
25 return r
== 0 ? -1 : 1;
29 stdio_seek(int64_t offset
, int whence
, void * fp
)
31 return fseek(fp
, offset
, whence
);
41 log_callback(nestegg
* ctx
, unsigned int severity
, char const * fmt
, ...)
44 char const * sev
= NULL
;
47 if (severity
< NESTEGG_LOG_WARNING
)
52 case NESTEGG_LOG_DEBUG
:
55 case NESTEGG_LOG_WARNING
:
58 case NESTEGG_LOG_CRITICAL
:
65 fprintf(stderr
, "%p %s ", (void *) ctx
, sev
);
68 vfprintf(stderr
, fmt
, ap
);
71 fprintf(stderr
, "\n");
75 main(int argc
, char * argv
[])
80 nestegg_audio_params aparams
;
82 nestegg_video_params vparams
;
84 uint64_t duration
, tstamp
, pkt_tstamp
;
85 unsigned char * codec_data
, * ptr
;
86 unsigned int cnt
, i
, j
, track
, tracks
, pkt_cnt
, pkt_track
;
87 unsigned int data_items
= 0;
98 fp
= fopen(argv
[1], "rb");
105 r
= nestegg_init(&ctx
, io
, log_callback
);
109 nestegg_track_count(ctx
, &tracks
);
110 nestegg_duration(ctx
, &duration
);
112 fprintf(stderr
, "media has %u tracks and duration %fs\n", tracks
, duration
/ 1e9
);
115 for (i
= 0; i
< tracks
; ++i
) {
116 type
= nestegg_track_type(ctx
, i
);
118 fprintf(stderr
, "track %u: type: %d codec: %d", i
,
119 type
, nestegg_track_codec_id(ctx
, i
));
121 nestegg_track_codec_data_count(ctx
, i
, &data_items
);
122 for (j
= 0; j
< data_items
; ++j
) {
123 nestegg_track_codec_data(ctx
, i
, j
, &codec_data
, &length
);
125 fprintf(stderr
, " (%p, %u)", codec_data
, (unsigned int) length
);
128 if (type
== NESTEGG_TRACK_VIDEO
) {
129 nestegg_track_video_params(ctx
, i
, &vparams
);
131 fprintf(stderr
, " video: %ux%u (d: %ux%u %ux%ux%ux%u)",
132 vparams
.width
, vparams
.height
,
133 vparams
.display_width
, vparams
.display_height
,
134 vparams
.crop_top
, vparams
.crop_left
, vparams
.crop_bottom
, vparams
.crop_right
);
136 } else if (type
== NESTEGG_TRACK_AUDIO
) {
137 nestegg_track_audio_params(ctx
, i
, &aparams
);
139 fprintf(stderr
, " audio: %.2fhz %u bit %u channels",
140 aparams
.rate
, aparams
.depth
, aparams
.channels
);
144 fprintf(stderr
, "\n");
150 fprintf(stderr
, "seek to middle\n");
152 r
= nestegg_track_seek(ctx
, 0, duration
/ 2);
155 fprintf(stderr
, "middle ");
157 r
= nestegg_read_packet(ctx
, &pkt
);
159 nestegg_packet_track(pkt
, &track
);
160 nestegg_packet_count(pkt
, &cnt
);
161 nestegg_packet_tstamp(pkt
, &tstamp
);
163 fprintf(stderr
, "* t %u pts %f frames %u\n", track
, tstamp
/ 1e9
, cnt
);
165 nestegg_free_packet(pkt
);
168 fprintf(stderr
, "middle seek failed\n");
174 fprintf(stderr
, "seek to ~end\n");
176 r
= nestegg_track_seek(ctx
, 0, duration
- (duration
/ 10));
179 fprintf(stderr
, "end ");
181 r
= nestegg_read_packet(ctx
, &pkt
);
183 nestegg_packet_track(pkt
, &track
);
184 nestegg_packet_count(pkt
, &cnt
);
185 nestegg_packet_tstamp(pkt
, &tstamp
);
187 fprintf(stderr
, "* t %u pts %f frames %u\n", track
, tstamp
/ 1e9
, cnt
);
189 nestegg_free_packet(pkt
);
192 fprintf(stderr
, "end seek failed\n");
198 fprintf(stderr
, "seek to ~start\n");
200 r
= nestegg_track_seek(ctx
, 0, duration
/ 10);
203 fprintf(stderr
, "start ");
205 r
= nestegg_read_packet(ctx
, &pkt
);
207 nestegg_packet_track(pkt
, &track
);
208 nestegg_packet_count(pkt
, &cnt
);
209 nestegg_packet_tstamp(pkt
, &tstamp
);
211 fprintf(stderr
, "* t %u pts %f frames %u\n", track
, tstamp
/ 1e9
, cnt
);
213 nestegg_free_packet(pkt
);
216 fprintf(stderr
, "start seek failed\n");
222 while (nestegg_read_packet(ctx
, &pkt
) > 0) {
223 nestegg_packet_track(pkt
, &pkt_track
);
224 nestegg_packet_count(pkt
, &pkt_cnt
);
225 nestegg_packet_tstamp(pkt
, &pkt_tstamp
);
228 fprintf(stderr
, "t %u pts %f frames %u: ", pkt_track
, pkt_tstamp
/ 1e9
, pkt_cnt
);
231 for (i
= 0; i
< pkt_cnt
; ++i
) {
232 nestegg_packet_data(pkt
, i
, &ptr
, &size
);
234 fprintf(stderr
, "%u ", (unsigned int) size
);
238 fprintf(stderr
, "\n");
241 nestegg_free_packet(pkt
);
244 nestegg_destroy(ctx
);