change commented out code that will never be executed again
[sparrow.git] / play.c
bloba965a86fb44e7a8e3cbaf7debb74d0168039fa2e
1 /* Copyright (C) <2010> Douglas Bagnall <douglas@halo.gen.nz>
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
19 #include "sparrow.h"
20 #include "gstsparrow.h"
22 #include <string.h>
23 #include <math.h>
24 #include "play_core.h"
26 #define one_subpixel one_subpixel_gamma_clamp_oldpix
28 static inline void
29 do_one_pixel(sparrow_play_t *player, guint8 *outpix, guint8 *inpix, guint8 *jpegpix,
30 guint8 *oldframe){
31 outpix[0] = one_subpixel(player, inpix[0], jpegpix[0], oldframe[0]);
32 outpix[1] = one_subpixel(player, inpix[1], jpegpix[1], oldframe[1]);
33 outpix[2] = one_subpixel(player, inpix[2], jpegpix[2], oldframe[2]);
34 outpix[3] = one_subpixel(player, inpix[3], jpegpix[3], oldframe[3]);
37 static inline int
38 select_jpeg_adaptive(GstSparrow *sparrow, sparrow_play_t *player){
40 GST_DEBUG("in select_jpeg_adaptive, starting at %d", player->jpeg_index);
41 GstBuffer *oldbuf = player->old_frames[player->old_frames_tail];
42 if (! oldbuf){
43 GST_DEBUG("old buffer is not there! using random jump: %d to %d",
44 player->jpeg_index, player->jpeg_index);
45 return RANDINT(sparrow, 0, sparrow->shared->image_count);
47 guint8 *old_frame = (guint8 *)GST_BUFFER_DATA(oldbuf);
49 cvResize( const CvArr* src, CvArr* dst, CV_INTER_LINEAR)
51 int x, y;
52 guint32 summary[6 * 8 * PIXSIZE] = {0};
53 int xdiv = sparrow->out.width / 8;
54 int ydiv = sparrow->out.height / 6;
55 int y2 = 0;
56 int x2 = 0;
57 for (y2 = 0; y2 < 6; y2++){
58 for (y = y2 * ydiv; y < (y2 + 1) * ydiv; y += 4){
59 for (x2 = 0; x2 < 8; x2++){
60 for (x = x2 * xdiv; x < (x2 + 1) * xdiv; x++){
61 int i2 = y2 * 8 + x2;
62 int i = y * sparrow->out.width + x;
64 summary[i2 + ] += old_frame[];
69 for (x = 0; x < sparrow->out.width; x++){
74 GST_DEBUG("in select_jpeg_adaptive");
76 return RANDINT(sparrow, 0, sparrow->shared->image_count);
80 static void
81 set_up_jpeg(GstSparrow *sparrow, sparrow_play_t *player){
82 /*XXX pick a jpeg, somehow*/
83 /*first, chance of random jump anywhere. */
84 if (rng_uniform(sparrow) < 1.0 / 500){
85 player->jpeg_index = RANDINT(sparrow, 0, sparrow->shared->image_count);
86 GST_DEBUG("CHANGE_SHOT: random jump: %d to %d", player->jpeg_index, player->jpeg_index);
88 else {
89 sparrow_frame_t *old_frame = &sparrow->shared->index[player->jpeg_index];
90 int next = old_frame->successors[0];
91 if (!next){
92 int i = RANDINT(sparrow, 1, 8);
93 if (i < 8){
94 next = old_frame->successors[i];
95 GST_DEBUG("CHANGE_SHOT: %d to %d (option %d)", player->jpeg_index, next, i);
97 else{
98 next = select_jpeg_adaptive(sparrow, player);
101 player->jpeg_index = next;
104 sparrow_frame_t *frame = &sparrow->shared->index[player->jpeg_index];
105 guint8 *src = sparrow->shared->jpeg_blob + frame->offset;
106 guint size = frame->jpeg_size;
107 GST_DEBUG("blob is %p, offset %d, src %p, size %d\n",
108 sparrow->shared->jpeg_blob, frame->offset, src, size);
110 begin_reading_jpeg(sparrow, src, size);
113 static void
114 play_from_full_lut(GstSparrow *sparrow, guint8 *in, guint8 *out){
115 sparrow_play_t *player = sparrow->helper_struct;
116 guint i;
117 int ox, oy;
118 guint32 *out32 = (guint32 *)out;
119 guint32 *in32 = (guint32 *)in;
120 GstBuffer *oldbuf = player->old_frames[player->old_frames_tail];
121 guint8 *old_frame = (oldbuf) ? (guint8 *)GST_BUFFER_DATA(oldbuf) : out;
123 set_up_jpeg(sparrow, player);
125 guint8 *jpeg_row = player->image_row;
126 i = 0;
127 for (oy = 0; oy < sparrow->out.height; oy++){
128 read_one_line(sparrow, jpeg_row);
129 for (ox = 0; ox < sparrow->out.width; ox++, i++){
130 guint inpos = sparrow->map_lut[i];
131 if (inpos){
132 do_one_pixel(player,
133 &out[i * PIXSIZE],
134 (guint8 *)&in32[inpos],
135 &jpeg_row[ox * PIXSIZE],
136 &old_frame[i * PIXSIZE]
139 else {
140 out32[i] = 0;
144 finish_reading_jpeg(sparrow);
146 if (DEBUG_PLAY && sparrow->debug){
147 debug_frame(sparrow, out, sparrow->out.width, sparrow->out.height, PIXSIZE);
151 static void
152 store_old_frame(GstSparrow *sparrow, GstBuffer *outbuf){
153 sparrow_play_t *player = sparrow->helper_struct;
154 player->old_frames[player->old_frames_head] = outbuf;
155 gst_buffer_ref(outbuf);
156 player->old_frames_head++;
157 player->old_frames_head %= OLD_FRAMES;
160 static void
161 drop_old_frame(GstSparrow *sparrow, GstBuffer *outbuf){
162 sparrow_play_t *player = sparrow->helper_struct;
163 GstBuffer *tail = player->old_frames[player->old_frames_tail];
164 if (tail){
165 gst_buffer_unref(tail);
167 player->old_frames_tail++;
168 player->old_frames_tail %= OLD_FRAMES;
172 INVISIBLE sparrow_state
173 mode_play(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf){
174 guint8 *in = GST_BUFFER_DATA(inbuf);
175 guint8 *out = GST_BUFFER_DATA(outbuf);
176 store_old_frame(sparrow, outbuf);
177 play_from_full_lut(sparrow, in, out);
178 drop_old_frame(sparrow, outbuf);
179 return SPARROW_STATUS_QUO;
182 static const double GAMMA = 2.0;
183 static const double INV_GAMMA = 1.0 / 2.0;
184 static const double FALSE_CEILING = 275;
186 static void
187 init_gamma_lut(sparrow_play_t *player){
188 for (int i = 0; i < 256; i++){
189 /* for each colour:
190 1. perform inverse gamma calculation (-> linear colour space)
191 2. negate
192 3 undo gamma.
194 double x, y;
195 x = i / 255.01;
196 y = pow(x, INV_GAMMA) *255;
197 x = pow(x, GAMMA) * 255;
198 if (x > 255){
199 x = 255;
201 if (y > 255){
202 y = 255;
204 player->lut_hi[i] = (guint8)x;
205 player->lut_lo[i] = (guint8)y;
209 INVISIBLE void init_play(GstSparrow *sparrow){
210 GST_DEBUG("starting play mode\n");
211 init_jpeg_src(sparrow);
212 sparrow_play_t *player = zalloc_aligned_or_die(sizeof(sparrow_play_t));
213 player->image_row = zalloc_aligned_or_die(sparrow->out.width * PIXSIZE);
214 player->old_frames_head = MIN(sparrow->lag, OLD_FRAMES - 1) || 1;
215 GST_INFO("using old frame lag of %d\n", player->old_frames_head);
216 sparrow->helper_struct = player;
217 init_gamma_lut(player);
218 GST_DEBUG("finished init_play\n");
221 INVISIBLE void finalise_play(GstSparrow *sparrow){
222 GST_DEBUG("leaving play mode\n");