-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleAV.h
138 lines (101 loc) · 3.3 KB
/
SimpleAV.h
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
/* Copyright (C) 2011 by Chenguang Wang(wecing)
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __SIMPLEAV_H__DEFINED__
#define __SIMPLEAV_H__DEFINED__
// ***** SAMutex and SAQueue starts *****
#if defined(__unix__) || defined(__MACH__)
#include <pthread.h>
#define SAMutex pthread_mutex_t
#elif defined(_WIN32)
#include <windows.h>
#include <process.h>
#define SAMutex HANDLE
#endif
int SAMutex_init(SAMutex *);
int SAMutex_lock(SAMutex *);
int SAMutex_unlock(SAMutex *);
int SAMutex_destroy(SAMutex *);
typedef struct _SAQNode {
void *data;
struct _SAQNode *next;
} _SAQNode;
typedef struct SAQContext {
_SAQNode *head, *tail;
int nb;
} SAQContext;
SAQContext *SAQ_init(void);
int SAQ_push(SAQContext *, void *);
void *SAQ_pop(SAQContext *);
// ***** SAMutex and SAQueue ends *****
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#define SAABUFFER_SIZE ((AVCODEC_MAX_AUDIO_FRAME_SIZE) * 3 / 2)
typedef struct SAVideoPacket {
AVFrame *frame_ptr;
double pts;
} SAVideoPacket;
typedef struct SAAudioPacket {
uint8_t *abuffer;
int len;
double pts;
} SAAudioPacket;
// FIXME: hide this. it should be a blackbox to the client.
typedef struct SAContext {
char *filename;
SAMutex aq_lock_real, vpq_lock_real, apq_lock_real, packet_lock_real;
SAMutex *aq_lock;
SAQContext *aq_ctx;
SAMutex *vpq_lock, *apq_lock;
SAQContext *vpq_ctx, *apq_ctx;
SAMutex *packet_lock;
AVPacket pkt_temp;
AVFormatContext *avfmt_ctx_ptr;
AVCodecContext *a_codec_ctx, *v_codec_ctx;
AVStream *audio_st, *video_st;
AVCodec *a_codec, *v_codec;
int v_stream, a_stream;
AVFrame *frame;
int v_width, v_height;
int audio_eof, video_eof;
// used for guessing PTS only; explained better in the source code of SA_seek.
// (in short they are almost useless)
double video_clock, a_clock;
} SAContext;
// Initialize libav. return 0 on success.
// FIXME: make it void?
int SA_init(void);
SAContext *SA_open(char *filename);
void SA_close(SAContext *sa_ctx);
void SA_dump_info(SAContext *sa_ctx);
SAAudioPacket *SA_get_ap(SAContext *sa_ctx);
SAVideoPacket *SA_get_vp(SAContext *sa_ctx);
void SA_free_ap(SAAudioPacket *ap);
void SA_free_vp(SAVideoPacket *vp);
// seek video to the last key frame before the requested second.
// return 0 on success; else return -1.
//
// will seek to the beginning if seek_to is negative.
// FIXME: it will not do anything special if requested time is
// after end of the video. (you could say it's a feature too)
//
// obviously the seeking is not accurate; you could use SA_get_[av]p to
// implement accurate seeking -- keep asking for new ap/vp until the frame
// you want is out.
int SA_seek(SAContext *sa_ctx, double seek_to);
int SA_get_width(SAContext *sa_ctx);
int SA_get_height(SAContext *sa_ctx);
double SA_get_duration(SAContext *sa_ctx);
#endif
#ifdef __cplusplus
}
#endif