summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTuomas Virtanen <katajakasa@gmail.com>2017-09-22 15:14:58 +0300
committerTuomas Virtanen <katajakasa@gmail.com>2017-09-22 15:17:21 +0300
commitfdaaf05142e446f1047487763585c3a16801580c (patch)
tree8042ce6b2464a2bce4300c6a7b6a841209f24c6a /include
parentfd7bbceb6321250ed5d80103d13e9d7584b552ac (diff)
Split decoding to separate files
Diffstat (limited to 'include')
-rw-r--r--include/kitchensink/internal/kitaudio.h22
-rw-r--r--include/kitchensink/internal/kitcontrol.h24
-rw-r--r--include/kitchensink/internal/kitdecoder.h54
-rw-r--r--include/kitchensink/internal/kitlog.h11
-rw-r--r--include/kitchensink/internal/kitsubtitle.h20
-rw-r--r--include/kitchensink/internal/kitvideo.h18
-rw-r--r--include/kitchensink/kitformats.h6
-rw-r--r--include/kitchensink/kitplayer.h51
-rw-r--r--include/kitchensink/kitsource.h8
9 files changed, 95 insertions, 119 deletions
diff --git a/include/kitchensink/internal/kitaudio.h b/include/kitchensink/internal/kitaudio.h
index e1c9ffb..ac6753b 100644
--- a/include/kitchensink/internal/kitaudio.h
+++ b/include/kitchensink/internal/kitaudio.h
@@ -1,28 +1,12 @@
#ifndef KITAUDIO_H
#define KITAUDIO_H
-#define __STDC_FORMAT_MACROS
-#include <inttypes.h>
-#include <libavformat/avformat.h>
-
#include "kitchensink/kitconfig.h"
#include "kitchensink/kitformats.h"
#include "kitchensink/kitplayer.h"
-#include "kitchensink/internal/kitringbuffer.h"
-
-#define KIT_ABUFFERSIZE 64
-
-typedef struct Kit_AudioPacket {
- double pts;
- size_t original_size;
- Kit_RingBuffer *rb;
-} Kit_AudioPacket;
+#include "kitchensink/internal/kitdecoder.h"
-KIT_LOCAL Kit_AudioPacket* _CreateAudioPacket(const char* data, size_t len, double pts);
-KIT_LOCAL void _FreeAudioPacket(void *ptr);
-KIT_LOCAL enum AVSampleFormat _FindAVSampleFormat(int format);
-KIT_LOCAL unsigned int _FindAVChannelLayout(int channels);
-KIT_LOCAL void _FindAudioFormat(enum AVSampleFormat fmt, int *bytes, bool *is_signed, unsigned int *format);
-KIT_LOCAL void _HandleAudioPacket(Kit_Player *player, AVPacket *packet);
+KIT_LOCAL Kit_Decoder* Kit_CreateAudioDecoder(const Kit_Source *src, Kit_AudioFormat *format);
+KIT_LOCAL int Kit_GetAudioDecoderData(Kit_Decoder *dec, unsigned char *buf, int len);
#endif // KITAUDIO_H
diff --git a/include/kitchensink/internal/kitcontrol.h b/include/kitchensink/internal/kitcontrol.h
deleted file mode 100644
index eaad484..0000000
--- a/include/kitchensink/internal/kitcontrol.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef KITCONTROL_H
-#define KITCONTROL_H
-
-#include "kitchensink/kitconfig.h"
-#include "kitchensink/kitplayer.h"
-
-#define KIT_CBUFFERSIZE 8
-
-typedef enum Kit_ControlPacketType {
- KIT_CONTROL_SEEK,
- KIT_CONTROL_FLUSH
-} Kit_ControlPacketType;
-
-typedef struct Kit_ControlPacket {
- Kit_ControlPacketType type;
- double value1;
-} Kit_ControlPacket;
-
-KIT_LOCAL Kit_ControlPacket* _CreateControlPacket(Kit_ControlPacketType type, double value1);
-KIT_LOCAL void _FreeControlPacket(void *ptr);
-KIT_LOCAL void _HandleFlushCommand(Kit_Player *player, Kit_ControlPacket *packet);
-KIT_LOCAL void _HandleSeekCommand(Kit_Player *player, Kit_ControlPacket *packet);
-
-#endif // KITCONTROL_H
diff --git a/include/kitchensink/internal/kitdecoder.h b/include/kitchensink/internal/kitdecoder.h
new file mode 100644
index 0000000..9d6f84d
--- /dev/null
+++ b/include/kitchensink/internal/kitdecoder.h
@@ -0,0 +1,54 @@
+#ifndef KITDECODER_H
+#define KITDECODER_H
+
+#include <stdbool.h>
+
+#include <SDL2/SDL_mutex.h>
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+
+#include "kitchensink/kitconfig.h"
+#include "kitchensink/kitsource.h"
+#include "kitchensink/internal/kitbuffer.h"
+
+#define KIT_DEC_IN 0
+#define KIT_DEC_OUT 1
+
+typedef struct Kit_Decoder Kit_Decoder;
+
+typedef int (*dec_decode_cb)(Kit_Decoder *dec, AVPacket *in_packet);
+typedef void (*dec_close_cb)(Kit_Decoder *dec);
+typedef void (*dec_free_packet_cb)(void *packet);
+
+KIT_LOCAL struct Kit_Decoder {
+ int stream_index; ///< Source stream index for the current stream
+ double clock_sync; ///< Sync source for current stream
+ double clock_pos; ///< Current pts for the stream
+
+ AVCodecContext *codec_ctx; ///< FFMpeg internal: Codec context
+ AVFormatContext *format_ctx; ///< FFMpeg internal: Format context (owner: Kit_Source)
+
+ SDL_mutex *lock[2]; ///< Threading locks for input and output buffers
+ Kit_Buffer *buffer[2]; ///< Buffers for incoming and decoded packets
+
+ void *userdata; ///< Decoder specific information (Audio, video, subtitle context)
+ dec_decode_cb dec_decode; ///< Decoder decoding function callback
+ dec_close_cb dec_close; ///< Decoder close function callback
+};
+
+KIT_LOCAL Kit_Decoder* Kit_CreateDecoder(const Kit_Source *src, int stream_index,
+ int in_b_size, int out_b_size,
+ dec_free_packet_cb free_out_cb);
+KIT_LOCAL void Kit_SetDecoderClockSync(Kit_Decoder *dec, double sync);
+KIT_LOCAL void Kit_ChangeDecoderClockSync(Kit_Decoder *dec, double sync);
+KIT_LOCAL bool Kit_CanWriteDecoderInput(Kit_Decoder *dec);
+KIT_LOCAL int Kit_WriteDecoderInput(Kit_Decoder *dec, AVPacket *packet);
+KIT_LOCAL AVPacket* Kit_ReadDecoderInput(Kit_Decoder *dec);
+KIT_LOCAL int Kit_WriteDecoderOutput(Kit_Decoder *dec, void *packet);
+KIT_LOCAL void* Kit_PeekDecoderOutput(Kit_Decoder *dec);
+KIT_LOCAL void Kit_AdvanceDecoderOutput(Kit_Decoder *dec);
+KIT_LOCAL void Kit_ClearDecoderBuffers(Kit_Decoder *dec);
+KIT_LOCAL int Kit_RunDecoder(Kit_Decoder *dec);
+KIT_LOCAL void Kit_CloseDecoder(Kit_Decoder *dec);
+
+#endif // KITDECODER_H
diff --git a/include/kitchensink/internal/kitlog.h b/include/kitchensink/internal/kitlog.h
new file mode 100644
index 0000000..1a56e53
--- /dev/null
+++ b/include/kitchensink/internal/kitlog.h
@@ -0,0 +1,11 @@
+#ifndef KITLOG_H
+#define KITLOG_H
+
+#ifdef NDEBUG
+ #define LOG(...)
+#else
+ #include <stdio.h>
+ #define LOG(...) fprintf(stderr, __VA_ARGS__)
+#endif
+
+#endif // KITLOG_H
diff --git a/include/kitchensink/internal/kitsubtitle.h b/include/kitchensink/internal/kitsubtitle.h
index 4582744..5296aef 100644
--- a/include/kitchensink/internal/kitsubtitle.h
+++ b/include/kitchensink/internal/kitsubtitle.h
@@ -1,24 +1,14 @@
#ifndef KITSUBTITLE_H
#define KITSUBTITLE_H
-#include <libavformat/avformat.h>
+#include <SDL2/SDL_render.h>
#include "kitchensink/kitconfig.h"
+#include "kitchensink/kitformats.h"
#include "kitchensink/kitplayer.h"
+#include "kitchensink/internal/kitdecoder.h"
-#define KIT_SBUFFERSIZE 512
-
-typedef struct Kit_SubtitlePacket {
- double pts_start;
- double pts_end;
- SDL_Rect *rect;
- SDL_Surface *surface;
- SDL_Texture *texture;
-} Kit_SubtitlePacket;
-
-KIT_LOCAL Kit_SubtitlePacket* _CreateSubtitlePacket(double pts_start, double pts_end, SDL_Rect *rect, SDL_Surface *surface);
-KIT_LOCAL void _FreeSubtitlePacket(void *ptr);
-KIT_LOCAL void _HandleBitmapSubtitle(Kit_SubtitlePacket** spackets, int *n, Kit_Player *player, double pts, AVSubtitle *sub, AVSubtitleRect *rect);
-KIT_LOCAL void _HandleSubtitlePacket(Kit_Player *player, AVPacket *packet);
+KIT_LOCAL Kit_Decoder* Kit_CreateSubtitleDecoder(const Kit_Source *src, Kit_SubtitleFormat *format, int w, int h);
+KIT_LOCAL int Kit_GetSubtitleDecoderData(Kit_Decoder *dec, SDL_Renderer *renderer);
#endif // KITSUBTITLE_H
diff --git a/include/kitchensink/internal/kitvideo.h b/include/kitchensink/internal/kitvideo.h
index f1cb4ff..5a5624f 100644
--- a/include/kitchensink/internal/kitvideo.h
+++ b/include/kitchensink/internal/kitvideo.h
@@ -1,22 +1,12 @@
#ifndef KITVIDEO_H
#define KITVIDEO_H
-#include <libavformat/avformat.h>
-
#include "kitchensink/kitconfig.h"
+#include "kitchensink/kitformats.h"
#include "kitchensink/kitplayer.h"
+#include "kitchensink/internal/kitdecoder.h"
-#define KIT_VBUFFERSIZE 3
-
-typedef struct Kit_VideoPacket {
- double pts;
- AVFrame *frame;
-} Kit_VideoPacket;
-
-KIT_LOCAL Kit_VideoPacket* _CreateVideoPacket(AVFrame *frame, double pts);
-KIT_LOCAL void _FreeVideoPacket(void *ptr);
-KIT_LOCAL void _FindPixelFormat(enum AVPixelFormat fmt, unsigned int *out_fmt);
-KIT_LOCAL enum AVPixelFormat _FindAVPixelFormat(unsigned int fmt);
-KIT_LOCAL void _HandleVideoPacket(Kit_Player *player, AVPacket *packet);
+KIT_LOCAL Kit_Decoder* Kit_CreateVideoDecoder(const Kit_Source *src, Kit_VideoFormat *format);
+KIT_LOCAL int Kit_GetVideoDecoderData(Kit_Decoder *dec, SDL_Texture *texture);
#endif // KITVIDEO_H
diff --git a/include/kitchensink/kitformats.h b/include/kitchensink/kitformats.h
index ef86826..a1cc61e 100644
--- a/include/kitchensink/kitformats.h
+++ b/include/kitchensink/kitformats.h
@@ -4,7 +4,7 @@
#include <stdbool.h>
typedef struct Kit_AudioFormat {
- int stream_idx; ///< Stream index
+ int stream_index; ///< Stream index
bool is_enabled; ///< Is stream enabled
unsigned int format; ///< SDL Audio Format
bool is_signed; ///< Signedness
@@ -14,7 +14,7 @@ typedef struct Kit_AudioFormat {
} Kit_AudioFormat;
typedef struct Kit_VideoFormat {
- int stream_idx; ///< Stream index
+ int stream_index; ///< Stream index
bool is_enabled; ///< Is stream enabled
unsigned int format; ///< SDL Pixel Format
int width; ///< Width in pixels
@@ -22,7 +22,7 @@ typedef struct Kit_VideoFormat {
} Kit_VideoFormat;
typedef struct Kit_SubtitleFormat {
- int stream_idx; ///< Stream index
+ int stream_index; ///< Stream index
bool is_enabled; ///< Is stream enabled
} Kit_SubtitleFormat;
diff --git a/include/kitchensink/kitplayer.h b/include/kitchensink/kitplayer.h
index 9505208..a33828b 100644
--- a/include/kitchensink/kitplayer.h
+++ b/include/kitchensink/kitplayer.h
@@ -8,6 +8,7 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_thread.h>
#include <SDL2/SDL_surface.h>
+#include <SDL2/SDL_mutex.h>
#include <stdbool.h>
@@ -26,47 +27,17 @@ typedef enum Kit_PlayerState {
} Kit_PlayerState;
typedef struct Kit_Player {
- // Local state
- Kit_PlayerState state; ///< Playback state
- Kit_VideoFormat vformat; ///< Video format information
- Kit_AudioFormat aformat; ///< Audio format information
+ Kit_PlayerState state; ///< Playback state
+ Kit_VideoFormat vformat; ///< Video format information
+ Kit_AudioFormat aformat; ///< Audio format information
Kit_SubtitleFormat sformat; ///< Subtitle format information
-
- // Synchronization
- double clock_sync; ///< Clock sync point
- double pause_start; ///< Timestamp of pause beginning
- double vclock_pos; ///< Video stream last pts
-
- // Threading
- SDL_Thread *dec_thread; ///< Decoder thread
- SDL_mutex *vmutex; ///< Video stream buffer lock
- SDL_mutex *amutex; ///< Audio stream buffer lock
- SDL_mutex *smutex; ///< Subtitle stream buffer lock
- SDL_mutex *cmutex; ///< Control stream buffer lock
-
- // Buffers
- void *abuffer; ///< Audio stream buffer
- void *vbuffer; ///< Video stream buffer
- void *sbuffer; ///< Subtitle stream buffer
- void *cbuffer; ///< Control stream buffer
-
- // FFmpeg internal state
- void *vcodec_ctx; ///< FFmpeg: Video codec context
- void *acodec_ctx; ///< FFmpeg: Audio codec context
- void *scodec_ctx; ///< FFmpeg: Subtitle codec context
- void *tmp_vframe; ///< FFmpeg: Preallocated temporary video frame
- void *tmp_aframe; ///< FFmpeg: Preallocated temporary audio frame
- void *tmp_sframe; ///< FFmpeg: Preallocated temporary subtitle frame
- void *swr; ///< FFmpeg: Audio resampler
- void *sws; ///< FFmpeg: Video converter
-
- // libass
- void *ass_renderer;
- void *ass_track;
-
- // Other
- uint8_t seek_flag;
- const Kit_Source *src; ///< Reference to Audio/Video source
+ void *audio_dec; ///< Audio decoder context (or NULL)
+ void *video_dec; ///< Video decoder context (or NULL)
+ void *subtitle_dec; ///< Subtitle decoder context (or NULL)
+ SDL_Thread *dec_thread; ///< Decoder thread
+ SDL_mutex *dec_lock; ///< Decoder lock
+ const Kit_Source *src; ///< Reference to Audio/Video source
+ double pause_started; ///< Temporary flag for handling pauses
} Kit_Player;
typedef struct Kit_PlayerInfo {
diff --git a/include/kitchensink/kitsource.h b/include/kitchensink/kitsource.h
index ed1711f..ea85d57 100644
--- a/include/kitchensink/kitsource.h
+++ b/include/kitchensink/kitsource.h
@@ -20,10 +20,10 @@ typedef enum Kit_StreamType {
} Kit_StreamType;
typedef struct Kit_Source {
- int astream_idx; ///< Audio stream index
- int vstream_idx; ///< Video stream index
- int sstream_idx; ///< Subtitle stream index
- void *format_ctx; ///< FFmpeg: Videostream format context
+ int audio_stream_index; ///< Audio stream index
+ int video_stream_index; ///< Video stream index
+ int subtitle_stream_index; ///< Subtitle stream index
+ void *format_ctx; ///< FFmpeg: Videostream format context
} Kit_Source;
typedef struct Kit_Stream {