summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/example_audio.c2
-rw-r--r--examples/example_video.c11
-rw-r--r--include/kitchensink/internal/subtitle/kitatlas.h3
-rw-r--r--src/internal/subtitle/kitatlas.c58
-rw-r--r--src/internal/subtitle/kitsubtitle.c1
-rw-r--r--src/internal/subtitle/renderers/kitsubass.c1
-rw-r--r--src/internal/subtitle/renderers/kitsubimage.c7
-rw-r--r--src/kitplayer.c2
8 files changed, 40 insertions, 45 deletions
diff --git a/examples/example_audio.c b/examples/example_audio.c
index 92a1397..45cf43d 100644
--- a/examples/example_audio.c
+++ b/examples/example_audio.c
@@ -4,8 +4,6 @@
#include <stdbool.h>
/*
-* Requires SDL2 2.0.4 !
-*
* Note! This example does not do proper error handling etc.
* It is for example use only!
*/
diff --git a/examples/example_video.c b/examples/example_video.c
index 2b95b7d..72467ab 100644
--- a/examples/example_video.c
+++ b/examples/example_video.c
@@ -4,8 +4,6 @@
#include <stdbool.h>
/*
-* Requires SDL2 2.0.4 !
-*
* Note! This example does not do proper error handling etc.
* It is for example use only!
*/
@@ -96,9 +94,6 @@ int main(int argc, char *argv[]) {
return 1;
}
- // Ask for linear texture scaling (better quality)
- SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
-
// Initialize Kitchensink with network and libass support.
err = Kit_Init(KIT_INIT_NETWORK|KIT_INIT_ASS);
if(err != 0) {
@@ -178,7 +173,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Subtitle format: %s\n", Kit_GetSDLPixelFormatString(pinfo.subtitle.format));
fflush(stderr);
- // Initialize textures
+ // Initialize video texture. This will probably end up as YV12 most of the time.
SDL_Texture *video_tex = SDL_CreateTexture(
renderer,
pinfo.video.format,
@@ -189,6 +184,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Error while attempting to create a video texture\n");
return 1;
}
+
+ // This is the subtitle texture atlas. This contains all the subtitle image fragments.
SDL_Texture *subtitle_tex = SDL_CreateTexture(
renderer,
pinfo.subtitle.format,
@@ -312,7 +309,7 @@ int main(int argc, char *argv[]) {
for(int i = 0; i < got; i++) {
SDL_RenderCopy(renderer, subtitle_tex, &sources[i], &targets[i]);
}
-
+
// Render GUI
if(gui_enabled) {
double percent = Kit_GetPlayerPosition(player) / Kit_GetPlayerDuration(player);
diff --git a/include/kitchensink/internal/subtitle/kitatlas.h b/include/kitchensink/internal/subtitle/kitatlas.h
index 2058f65..b43db2f 100644
--- a/include/kitchensink/internal/subtitle/kitatlas.h
+++ b/include/kitchensink/internal/subtitle/kitatlas.h
@@ -34,7 +34,6 @@ KIT_LOCAL void Kit_RefreshAtlas(Kit_TextureAtlas *atlas, double current_pts);
KIT_LOCAL void Kit_ClearAtlasContent(Kit_TextureAtlas *atlas);
KIT_LOCAL int Kit_UpdateAtlasTexture(Kit_TextureAtlas *atlas, SDL_Texture *texture);
KIT_LOCAL int Kit_GetAtlasItems(const Kit_TextureAtlas *atlas, SDL_Rect *sources, SDL_Rect *targets, int limit);
-KIT_LOCAL int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, SDL_Rect *target);
-KIT_LOCAL void Kit_FreeAtlasItem(Kit_TextureAtlas *atlas, int item_id);
+KIT_LOCAL int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, const SDL_Rect *target);
#endif // KITATLAS_H
diff --git a/src/internal/subtitle/kitatlas.c b/src/internal/subtitle/kitatlas.c
index 09993b2..a62690e 100644
--- a/src/internal/subtitle/kitatlas.c
+++ b/src/internal/subtitle/kitatlas.c
@@ -68,24 +68,28 @@ void Kit_FreeAtlas(Kit_TextureAtlas *atlas) {
}
void Kit_SetItemAllocation(Kit_TextureAtlasItem *item, int shelf, int slot, int x, int y) {
+ assert(item != NULL);
+
item->cur_shelf = shelf;
item->cur_slot = slot;
item->source.x = x;
item->source.y = y;
item->source.w = item->surface->w;
item->source.h = item->surface->h;
- //LOG("Writing x,y=(%d, %d) w,h=(%d, %d) on shelf %d slot %d\n",
- // x, y, item->surface->w, item->surface->h, shelf, slot);
}
int Kit_FindFreeAtlasSlot(Kit_TextureAtlas *atlas, Kit_TextureAtlasItem *item) {
+ assert(atlas != NULL);
+ assert(item != NULL);
+
int shelf_w, shelf_h;
int total_remaining_h = atlas->h;
int total_reserved_h = 0;
// First, try to look for a good, existing shelf
int best_shelf_idx = -1;
- int best_shelf_h = 0;
+ int best_shelf_h = atlas->h;
+ int best_shelf_y = 0;
// Try to find a good shelf to put this item in
int shelf_idx;
@@ -100,9 +104,10 @@ int Kit_FindFreeAtlasSlot(Kit_TextureAtlas *atlas, Kit_TextureAtlasItem *item) {
// If the item fits, check if the space is better than previous one
if(item->surface->w <= (atlas->w - shelf_w) && item->surface->h <= shelf_h) {
- if(shelf_h > best_shelf_h) {
+ if(shelf_h < best_shelf_h) {
best_shelf_h = shelf_h;
best_shelf_idx = shelf_idx;
+ best_shelf_y = total_reserved_h - shelf_h;
}
}
}
@@ -114,11 +119,11 @@ int Kit_FindFreeAtlasSlot(Kit_TextureAtlas *atlas, Kit_TextureAtlasItem *item) {
best_shelf_idx,
atlas->shelf[best_shelf_idx][2],
atlas->shelf[best_shelf_idx][0],
- total_reserved_h);
+ best_shelf_y);
atlas->shelf[best_shelf_idx][0] += item->surface->w;
atlas->shelf[best_shelf_idx][2] += 1;
return 0;
- } else if(total_remaining_h > item->surface->h) {
+ } else if(total_remaining_h >= item->surface->h) {
atlas->shelf[shelf_idx][0] = item->surface->w;
atlas->shelf[shelf_idx][1] = item->surface->h;
atlas->shelf[shelf_idx][2] = 1;
@@ -134,29 +139,34 @@ int Kit_FindFreeAtlasSlot(Kit_TextureAtlas *atlas, Kit_TextureAtlasItem *item) {
return 1; // Can't fit!
}
-int Kit_AllocateAtlasSurfaces(Kit_TextureAtlas *atlas, bool halt_on_errors) {
- int ret = 0;
+int Kit_AllocateAtlasSurfaces(Kit_TextureAtlas *atlas) {
+ assert(atlas != NULL);
+
Kit_TextureAtlasItem *item;
for(int i = 0; i < atlas->cur_items; i++) {
item = &atlas->items[i];
if(item->cur_shelf > -1) // Stop if already allocated
continue;
if(Kit_FindFreeAtlasSlot(atlas, item) != 0) { // If nothing else helps, create a new slot
- ret = 1;
- if(halt_on_errors)
- goto exit_0;
+ goto exit_0;
}
}
+ return 0;
exit_0:
- return ret;
+ return 1;
}
void Kit_BlitAtlasSurfaces(Kit_TextureAtlas *atlas, SDL_Texture *texture) {
+ assert(atlas != NULL);
+ assert(texture != NULL);
+
Kit_TextureAtlasItem *item;
for(int i = 0; i < atlas->cur_items; i++) {
item = &atlas->items[i];
- if(!item->is_copied) { // Copy if not yet copied
+ if(item->cur_shelf == -1) // Skip if not allocated
+ continue;
+ if(!item->is_copied) {
SDL_UpdateTexture(texture, &item->source, item->surface->pixels, item->surface->pitch);
item->is_copied = true;
}
@@ -177,7 +187,7 @@ int Kit_UpdateAtlasTexture(Kit_TextureAtlas *atlas, SDL_Texture *texture) {
atlas->h = texture_h;
}
- if(Kit_AllocateAtlasSurfaces(atlas, true) != 0) {
+ if(Kit_AllocateAtlasSurfaces(atlas) != 0) {
LOG("WARNING! FULL ATLAS, CANNOT MAKE MORE ROOM!\n");
}
@@ -187,9 +197,14 @@ int Kit_UpdateAtlasTexture(Kit_TextureAtlas *atlas, SDL_Texture *texture) {
}
int Kit_GetAtlasItems(const Kit_TextureAtlas *atlas, SDL_Rect *sources, SDL_Rect *targets, int limit) {
+ assert(atlas != NULL);
+ assert(limit >= 0);
+
int count = 0;
for(int i = 0; i < min(atlas->cur_items, limit); i++) {
Kit_TextureAtlasItem *item = &atlas->items[i];
+ if(!item->is_copied)
+ continue;
if(sources != NULL)
memcpy(&sources[count], &item->source, sizeof(SDL_Rect));
@@ -201,7 +216,7 @@ int Kit_GetAtlasItems(const Kit_TextureAtlas *atlas, SDL_Rect *sources, SDL_Rect
return count;
}
-int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, SDL_Rect *target) {
+int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, const SDL_Rect *target) {
assert(atlas != NULL);
assert(surface != NULL);
assert(target != NULL);
@@ -212,7 +227,7 @@ int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, SDL_Rect *ta
return -1; // Cannot fit, buffer full!
}
- item = &atlas->items[atlas->cur_items];
+ item = &atlas->items[atlas->cur_items++];
item->cur_shelf = -1;
item->cur_slot = -1;
item->is_copied = false;
@@ -220,14 +235,5 @@ int Kit_AddAtlasItem(Kit_TextureAtlas *atlas, SDL_Surface *surface, SDL_Rect *ta
item->surface->refcount++; // We dont want to needlessly copy; instead increase refcount.
memset(&item->source, 0, sizeof(SDL_Rect));
memcpy(&item->target, target, sizeof(SDL_Rect));
- return atlas->cur_items++;
-}
-
-void Kit_FreeAtlasItem(Kit_TextureAtlas *atlas, int item_id) {
- assert(item_id >= 0);
- SDL_FreeSurface(atlas->items[item_id].surface);
- memmove(&atlas->items[item_id],
- &atlas->items[item_id+1],
- (atlas->max_items - item_id - 1) * sizeof(Kit_TextureAtlasItem));
- atlas->cur_items--;
+ return 0;
}
diff --git a/src/internal/subtitle/kitsubtitle.c b/src/internal/subtitle/kitsubtitle.c
index a1909ee..c1b5973 100644
--- a/src/internal/subtitle/kitsubtitle.c
+++ b/src/internal/subtitle/kitsubtitle.c
@@ -192,6 +192,5 @@ int Kit_GetSubtitleDecoderData(Kit_Decoder *dec, SDL_Texture *texture, SDL_Rect
int item_count = Kit_GetAtlasItems(subtitle_dec->atlas, sources, targets, limit);
// All done
- dec->clock_pos = sync_ts;
return item_count;
}
diff --git a/src/internal/subtitle/renderers/kitsubass.c b/src/internal/subtitle/renderers/kitsubass.c
index 8618a3d..88efcb6 100644
--- a/src/internal/subtitle/renderers/kitsubass.c
+++ b/src/internal/subtitle/renderers/kitsubass.c
@@ -98,6 +98,7 @@ static int ren_get_data_cb(Kit_SubtitleRenderer *ren, Kit_TextureAtlas *atlas, d
SDL_FreeSurface(dst);
}
+ ren->dec->clock_pos = current_pts;
return 0;
}
diff --git a/src/internal/subtitle/renderers/kitsubimage.c b/src/internal/subtitle/renderers/kitsubimage.c
index 40279a1..2edafc9 100644
--- a/src/internal/subtitle/renderers/kitsubimage.c
+++ b/src/internal/subtitle/renderers/kitsubimage.c
@@ -9,11 +9,6 @@
#include "kitchensink/internal/subtitle/kitsubtitlepacket.h"
#include "kitchensink/internal/subtitle/renderers/kitsubimage.h"
-typedef struct Kit_SubtitleCBData {
- Kit_TextureAtlas *atlas;
- double current_pts;
-} Kit_SubtitleCBData;
-
static void ren_render_image_cb(Kit_SubtitleRenderer *ren, void *sub_src, double start_pts, double end_pts) {
assert(ren != NULL);
assert(sub_src != NULL);
@@ -44,7 +39,6 @@ static void ren_render_image_cb(Kit_SubtitleRenderer *ren, void *sub_src, double
SDL_BlitSurface(src, NULL, dst, NULL);
// Create a new packet and write it to output buffer
- SDL_SetSurfaceBlendMode(dst, SDL_BLENDMODE_BLEND);
Kit_WriteDecoderOutput(
ren->dec, Kit_CreateSubtitlePacket(false, start_pts, end_pts, r->x, r->y, dst));
@@ -79,6 +73,7 @@ static int ren_get_data_cb(Kit_SubtitleRenderer *ren, Kit_TextureAtlas *atlas, d
}
Kit_AdvanceDecoderOutput(ren->dec);
Kit_FreeSubtitlePacket(packet);
+ ren->dec->clock_pos = current_pts;
continue;
}
break;
diff --git a/src/kitplayer.c b/src/kitplayer.c
index 82b4a71..42bfc3a 100644
--- a/src/kitplayer.c
+++ b/src/kitplayer.c
@@ -227,7 +227,7 @@ int Kit_GetSubtitleData(Kit_Player *player, SDL_Texture *texture, SDL_Rect *sour
return 0;
}
- // If paused or stopped, do nothing
+ // If stopped, do nothing
if(player->state == KIT_STOPPED) {
return 0;
}