player_example.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*Daala video codec
  2. Copyright (c) 2006-2010 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <daala/codec.h>
  23. #include <daala/daaladec.h>
  24. #include "SDL.h"
  25. #define PACKET_START 0
  26. #define PACKET_HEADER 1
  27. #define PACKET_DATA 2
  28. #define PACKET_DONE 3
  29. #define OD_MINI(_a,_b) ((_a)<(_b)?(_a):(_b))
  30. #define OD_MAXI(_a,_b) ((_a)>(_b)?(_a):(_b))
  31. #define OD_CLAMPI(_a,_b,_c) (OD_MAXI(_a,OD_MINI(_b,_c)))
  32. #define OD_SIGNMASK(_a) (-((_a)<0))
  33. #define OD_FLIPSIGNI(_a,_b) ((_a)+OD_SIGNMASK(_b)^OD_SIGNMASK(_b))
  34. #define OD_DIV_ROUND(_x,_y) (((_x)+OD_FLIPSIGNI((_y)>>1,_x))/(_y))
  35. #define OD_CLAMP255(_x) ((unsigned char)((((_x)<0)-1)&((_x)|-((_x)>255))))
  36. void img_to_rgb(SDL_Surface *surf, const od_img *img);
  37. int main(int argc, char *argv[]) {
  38. SDL_Surface *screen;
  39. SDL_Event event;
  40. int done;
  41. daala_info di;
  42. daala_comment dc;
  43. daala_setup_info *dsi;
  44. ogg_sync_state oy;
  45. ogg_page page;
  46. int ret;
  47. FILE *input;
  48. size_t bytes;
  49. int eof;
  50. int packet_state;
  51. ogg_stream_state os;
  52. ogg_packet packet;
  53. daala_dec_ctx *dctx;
  54. SDL_Surface *surf;
  55. od_img img;
  56. int frame;
  57. int paused;
  58. int step;
  59. if (argc != 2) {
  60. fprintf(stderr, "usage: %s input.ogg\n", argv[0]);
  61. exit(1);
  62. }
  63. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  64. fprintf(stderr, "error: unable to init SDL");
  65. exit(1);
  66. }
  67. atexit(SDL_Quit);
  68. screen = NULL;
  69. surf = NULL;
  70. paused = 1;
  71. restart:
  72. ret = ogg_sync_init(&oy);
  73. assert(ret == 0);
  74. daala_info_init(&di);
  75. daala_comment_init(&dc);
  76. dsi = NULL;
  77. packet_state = PACKET_START;
  78. frame = 0;
  79. done = 0;
  80. step = 0;
  81. input = fopen(argv[1], "rb");
  82. assert(input != NULL);
  83. eof = 0;
  84. while (!eof) {
  85. while (!eof && ogg_sync_pageout(&oy, &page) != 1) {
  86. char *buffer = ogg_sync_buffer(&oy, 4096);
  87. assert(buffer != NULL);
  88. bytes = fread(buffer, 1, 4096, input);
  89. if (bytes > 0) {
  90. ret = ogg_sync_wrote(&oy, bytes);
  91. assert(ret == 0);
  92. } else {
  93. eof = 1;
  94. }
  95. }
  96. if (packet_state == PACKET_START) {
  97. ret = ogg_stream_init(&os, ogg_page_serialno(&page));
  98. assert(ret == 0);
  99. packet_state = PACKET_HEADER;
  100. }
  101. ret = ogg_stream_pagein(&os, &page);
  102. assert(ret == 0);
  103. while (!done && ogg_stream_packetout(&os, &packet) == 1) {
  104. switch (packet_state) {
  105. case PACKET_HEADER:
  106. ret = daala_decode_header_in(&di, &dc, &dsi, &packet);
  107. assert(ret >= 0);
  108. if (ret != 0) break;
  109. dctx = daala_decode_alloc(&di, dsi);
  110. assert(dctx != NULL);
  111. /*daala_setup_free(dsi);*/
  112. packet_state = PACKET_DATA;
  113. case PACKET_DATA:
  114. if (screen == NULL) {
  115. screen = SDL_SetVideoMode(di.pic_width, di.pic_height, 24,
  116. SDL_SWSURFACE | SDL_DOUBLEBUF);
  117. assert(screen != NULL);
  118. surf = SDL_GetVideoSurface();
  119. assert(surf != NULL);
  120. }
  121. ret = daala_decode_packet_in(dctx, &img, &packet);
  122. assert(ret == 0);
  123. SDL_LockSurface(surf);
  124. img_to_rgb(surf, &img);
  125. SDL_UnlockSurface(surf);
  126. SDL_Flip(screen);
  127. if (step) {
  128. step = 0;
  129. paused = 1;
  130. }
  131. do {
  132. while (SDL_PollEvent(&event)) {
  133. switch (event.type) {
  134. case SDL_QUIT:
  135. done = 1;
  136. break;
  137. case SDL_KEYDOWN:
  138. switch (event.key.keysym.sym) {
  139. case SDLK_ESCAPE:
  140. done = 1;
  141. break;
  142. case SDLK_SPACE:
  143. paused = !paused;
  144. break;
  145. case SDLK_PERIOD:
  146. step = 1;
  147. paused = 0;
  148. break;
  149. case SDLK_r:
  150. done = 0;
  151. step = 0;
  152. goto restart;
  153. default:
  154. break;
  155. }
  156. }
  157. }
  158. } while (paused && !done);
  159. }
  160. }
  161. }
  162. while (!done) {
  163. while (SDL_PollEvent(&event)) {
  164. switch (event.type) {
  165. case SDL_QUIT:
  166. done = 1;
  167. break;
  168. case SDL_KEYDOWN:
  169. if (event.key.keysym.sym == SDLK_ESCAPE) done = 1;
  170. if (event.key.keysym.sym == SDLK_r) {
  171. paused = 0;
  172. done = 0;
  173. step = 0;
  174. goto restart;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. void img_to_rgb(SDL_Surface *surf, const od_img *img) {
  183. unsigned char *y_row;
  184. unsigned char *cb_row;
  185. unsigned char *cr_row;
  186. unsigned char *y;
  187. unsigned char *cb;
  188. unsigned char *cr;
  189. int y_stride;
  190. int cb_stride;
  191. int cr_stride;
  192. int width;
  193. int height;
  194. int xdec;
  195. int ydec;
  196. int i;
  197. int j;
  198. unsigned char *pixels;
  199. int pitch;
  200. pixels = (unsigned char *)surf->pixels;
  201. pitch = surf->pitch;
  202. width = img->width;
  203. height = img->height;
  204. /*Assume both C planes are decimated.*/
  205. xdec = img->planes[1].xdec;
  206. ydec = img->planes[1].ydec;
  207. y_stride = img->planes[0].ystride;
  208. cb_stride = img->planes[1].ystride;
  209. cr_stride = img->planes[2].ystride;
  210. y_row = img->planes[0].data;
  211. cb_row = img->planes[1].data;
  212. cr_row = img->planes[2].data;
  213. /*Chroma up-sampling is just done with a box filter.
  214. This is very likely what will actually be used in practice on a real
  215. display, and also removes one more layer to search in for the source of
  216. artifacts.
  217. As an added bonus, it's dead simple.*/
  218. for (j = 0; j < height; j++) {
  219. int dc;
  220. y = y_row;
  221. cb = cb_row;
  222. cr = cr_row;
  223. for (i = 0; i < 3 * width;) {
  224. int64_t yval;
  225. int64_t cbval;
  226. int64_t crval;
  227. unsigned rval;
  228. unsigned gval;
  229. unsigned bval;
  230. yval=*y-16;
  231. cbval=*cb-128;
  232. crval=*cr-128;
  233. /*This is intentionally slow and very accurate.*/
  234. rval=OD_CLAMPI(0,(int32_t)OD_DIV_ROUND(
  235. 2916394880000LL*yval+4490222169144LL*crval,9745792000LL),65535);
  236. gval=OD_CLAMPI(0,(int32_t)OD_DIV_ROUND(
  237. 2916394880000LL*yval-534117096223LL*cbval-1334761232047LL*crval,
  238. 9745792000LL),65535);
  239. bval=OD_CLAMPI(0,(int32_t)OD_DIV_ROUND(
  240. 2916394880000LL*yval+5290866304968LL*cbval,9745792000LL),65535);
  241. *(pixels + pitch*j + i++)=(unsigned char)(bval>>8);
  242. *(pixels + pitch*j + i++)=(unsigned char)(gval>>8);
  243. *(pixels + pitch*j + i++)=(unsigned char)(rval>>8);
  244. dc = y - y_row & 1 | 1 - xdec;
  245. y++;
  246. cb += dc;
  247. cr += dc;
  248. }
  249. y_row += y_stride;
  250. dc = -(j & 1 | 1 - ydec);
  251. cb_row += dc & cb_stride;
  252. cr_row += dc & cr_stride;
  253. }
  254. }