| 1 | #include <stdint.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <stdio.h>
|
|---|
| 4 | #include <string.h>
|
|---|
| 5 | #include <math.h>
|
|---|
| 6 | #include "libavutil/common.h"
|
|---|
| 7 | #include "libavutil/mathematics.h"
|
|---|
| 8 | #include "libavformat/avformat.h"
|
|---|
| 9 |
|
|---|
| 10 | // Number of frames to print packet.pts information for.
|
|---|
| 11 | static const int kFramesToPrint = 30;
|
|---|
| 12 |
|
|---|
| 13 | int main(int argc, char *argv[]) {
|
|---|
| 14 | AVFormatContext *ctx = avformat_alloc_context();
|
|---|
| 15 | avformat_close_input(&ctx);
|
|---|
| 16 |
|
|---|
| 17 | if (argc < 2) {
|
|---|
| 18 | printf("usage: ./ogg-seek <audio file>\n");
|
|---|
| 19 | return -1;
|
|---|
| 20 | }
|
|---|
| 21 | av_log_set_level(AV_LOG_QUIET);
|
|---|
| 22 |
|
|---|
| 23 | av_register_all();
|
|---|
| 24 | AVFormatContext *format_context = NULL;
|
|---|
| 25 | if (avformat_open_input(&format_context, argv[1], NULL, NULL) != 0)
|
|---|
| 26 | return -1;
|
|---|
| 27 |
|
|---|
| 28 | if (avformat_find_stream_info(format_context, NULL) < 0)
|
|---|
| 29 | return -1;
|
|---|
| 30 |
|
|---|
| 31 | // Find first audio stream.
|
|---|
| 32 | int audio_stream_index = -1;
|
|---|
| 33 | for (int i = 0; i < format_context->nb_streams; i++) {
|
|---|
| 34 | if (format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|---|
| 35 | audio_stream_index = i;
|
|---|
| 36 | break;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | if (audio_stream_index == -1)
|
|---|
| 41 | return -1;
|
|---|
| 42 |
|
|---|
| 43 | // Read a few frames
|
|---|
| 44 | int frames = 0, ret = 0;
|
|---|
| 45 | AVPacket packet = { 0 };
|
|---|
| 46 | while ((ret = av_read_frame(format_context, &packet)) >= 0 && frames < 25) {
|
|---|
| 47 | if (packet.stream_index == audio_stream_index) {
|
|---|
| 48 | frames++;
|
|---|
| 49 | printf("pts: %05ld -- dts: %05ld\n", packet.pts, packet.dts);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | printf("--------------------\n");
|
|---|
| 54 |
|
|---|
| 55 | // Seek ahead of granule @ 96000.
|
|---|
| 56 | if (avformat_seek_file(format_context, 0, INT64_MIN, 88000, INT64_MAX, AVSEEK_FLAG_BACKWARD) < 0)
|
|---|
| 57 | return -1;
|
|---|
| 58 |
|
|---|
| 59 | // Read all frames, but only print kFramesToPrint.
|
|---|
| 60 | int64_t last_pts = -1000;
|
|---|
| 61 | while ((ret = av_read_frame(format_context, &packet)) >= 0) {
|
|---|
| 62 | if (packet.stream_index == audio_stream_index) {
|
|---|
| 63 | printf("pts: %05ld -- dts: %05ld", packet.pts, packet.dts);
|
|---|
| 64 | if (packet.pts < last_pts){
|
|---|
| 65 | printf(" <------------ BACKWARDS!\n");
|
|---|
| 66 | break;
|
|---|
| 67 | } else {
|
|---|
| 68 | last_pts = packet.pts;
|
|---|
| 69 | printf("\n");
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | avformat_close_input(&format_context);
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|