summaryrefslogtreecommitdiff
path: root/src/video_decode.c
blob: 993fd148ad3b87cca797fbf11889190d503c6320 (plain)
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
#ifndef DEBUG
#define DEBUG 1
#endif

#include "video_decode.h"

#include <libavutil/avutil.h>
#include <libavutil/error.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>

struct video_frame_priv {
	struct video_frame base;
	AVFrame *frame;
};

int
video_decode_init(struct video_decode **vd_out, char *file, int64_t ts)
{
	struct video_decode *vd;

	vd = calloc(1, sizeof *vd);
	if (vd == NULL)
		return -1;

	av_register_all();
#if DEBUG == 0
	av_log_set_level(AV_LOG_QUIET);
#endif

	vd->fmt_ctx = NULL;
	vd->codec_ctx = NULL;

	if (avformat_open_input(&vd->fmt_ctx, file, NULL, NULL) != 0)
		return -1;

	if (avformat_find_stream_info(vd->fmt_ctx, NULL) < 0)
		return -2;
#if DEBUG
	av_dump_format(vd->fmt_ctx, 0, file, 0);
#endif
	vd->stream = av_find_best_stream(vd->fmt_ctx, AVMEDIA_TYPE_VIDEO,
					     -1, -1, NULL, 0);
	if (vd->stream == -1)
		return -3;

	vd->codec_ctx = vd->fmt_ctx->streams[vd->stream]->codec;

	if (vd->fmt_ctx->start_time != AV_NOPTS_VALUE)
		ts += vd->fmt_ctx->start_time;
	/* FIXME: Check size */
	avformat_seek_file(vd->fmt_ctx, vd->stream, INT64_MIN, ts, INT64_MAX,0);

	vd->codec = avcodec_find_decoder(vd->codec_ctx->codec_id);
	if (vd->codec == NULL) {
		fprintf(stderr, "Failed to find decoder!\n");
		return -4;
	}
#if DEBUG
	printf("has cap truncated: %d\n",
	       vd->codec->capabilities & CODEC_CAP_TRUNCATED);
#endif
#if 0
	if (codec->capabilities & CODEC_CAP_TRUNCATED)
		codec_ctx->flags |= CODEC_FLAG_TRUNCATED;
#endif
	if (avcodec_open2(vd->codec_ctx, vd->codec, NULL) < 0) {
		fprintf(stderr, "Failed to open codec!\n");
		return -5;
	}

	*vd_out = vd;
	return 0;
}

void
video_decode_uninit(struct video_decode **_vd)
{
	struct video_decode *vd = *_vd;

	*_vd = NULL;
	avcodec_close(vd->codec_ctx);
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 17, 0)
	avformat_close_input(&vd->fmt_ctx);
#else
	av_close_input_file(vd->fmt_ctx);
#endif
	free(vd);
}

int
video_decode_get_frame(struct video_decode *vd, struct video_frame **frame)
{
	struct video_frame_priv *f;
	AVPacket packet;
	int decoded, got_picture;
	int ret;

	f = calloc(1, sizeof *f);
	if (f == NULL)
		return -1;

	packet.data = NULL;
	do {
		if (packet.data != NULL)
			av_free_packet(&packet);
		ret = av_read_frame(vd->fmt_ctx, &packet);
		if (ret < 0) {
			if (ret == AVERROR_EOF) {
				return 0;
			}
#if DEBUG
			else {
				char buf[BUFSIZ];
				av_strerror(ret, buf, BUFSIZ);
				fprintf(stderr,
					"Failed to read frame: %s\n", buf);
			}
#endif
			return -1;
		}
	} while (packet.stream_index != vd->stream);

	f->frame = avcodec_alloc_frame();

	do {
#if DEBUG
		printf("packet.data: %p, packet.siz: %d, packet.strm_idx: %d\n",
		       packet.data, packet.size, packet.stream_index);
#endif
		decoded = avcodec_decode_video2(vd->codec_ctx, f->frame,
						&got_picture, &packet);
		if (decoded < 0) {
			fprintf(stderr, "Error while decoding frame!\n");
			return -2;
		}
#if DEBUG
		printf("bytes_decoded: %d, got_picture: %d\n",
		       decoded, got_picture);
#endif
		
	} while (got_picture == 0 && packet.size > 0);
	av_free_packet(&packet);
	if (got_picture == 0)
		return -3;

	f->base.data = f->frame->data[0];
	f->base.width = vd->codec_ctx->width;
	f->base.height = vd->codec_ctx->height;
	f->base.stride = f->frame->linesize[0];

	*frame = &f->base;

	return 1;
}

void
video_decode_free_frame(struct video_frame **frame)
{
	struct video_frame_priv *f = (struct video_frame_priv *) *frame;

	*frame = NULL;
	av_free(f->frame);
	free(f);
}