decapiwrapper.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id: decapiwrapper.c 13596 2007-08-23 20:05:38Z tterribe $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <limits.h>
  18. #include "apiwrapper.h"
  19. #include "decint.h"
  20. #include "theora/theoradec.h"
  21. static void th_dec_api_clear(th_api_wrapper *_api){
  22. if(_api->setup)th_setup_free(_api->setup);
  23. if(_api->decode)th_decode_free(_api->decode);
  24. memset(_api,0,sizeof(*_api));
  25. }
  26. static void theora_decode_clear(theora_state *_td){
  27. if(_td->i!=NULL)theora_info_clear(_td->i);
  28. memset(_td,0,sizeof(*_td));
  29. }
  30. static int theora_decode_control(theora_state *_td,int _req,
  31. void *_buf,size_t _buf_sz){
  32. return th_decode_ctl(((th_api_wrapper *)_td->i->codec_setup)->decode,
  33. _req,_buf,_buf_sz);
  34. }
  35. static ogg_int64_t theora_decode_granule_frame(theora_state *_td,
  36. ogg_int64_t _gp){
  37. return th_granule_frame(((th_api_wrapper *)_td->i->codec_setup)->decode,_gp);
  38. }
  39. static double theora_decode_granule_time(theora_state *_td,ogg_int64_t _gp){
  40. return th_granule_time(((th_api_wrapper *)_td->i->codec_setup)->decode,_gp);
  41. }
  42. static const oc_state_dispatch_vtable OC_DEC_DISPATCH_VTBL={
  43. (oc_state_clear_func)theora_decode_clear,
  44. (oc_state_control_func)theora_decode_control,
  45. (oc_state_granule_frame_func)theora_decode_granule_frame,
  46. (oc_state_granule_time_func)theora_decode_granule_time,
  47. };
  48. static void th_info2theora_info(theora_info *_ci,const th_info *_info){
  49. _ci->version_major=_info->version_major;
  50. _ci->version_minor=_info->version_minor;
  51. _ci->version_subminor=_info->version_subminor;
  52. _ci->width=_info->frame_width;
  53. _ci->height=_info->frame_height;
  54. _ci->frame_width=_info->pic_width;
  55. _ci->frame_height=_info->pic_height;
  56. _ci->offset_x=_info->pic_x;
  57. _ci->offset_y=_info->pic_y;
  58. _ci->fps_numerator=_info->fps_numerator;
  59. _ci->fps_denominator=_info->fps_denominator;
  60. _ci->aspect_numerator=_info->aspect_numerator;
  61. _ci->aspect_denominator=_info->aspect_denominator;
  62. switch(_info->colorspace){
  63. case TH_CS_ITU_REC_470M:_ci->colorspace=OC_CS_ITU_REC_470M;break;
  64. case TH_CS_ITU_REC_470BG:_ci->colorspace=OC_CS_ITU_REC_470BG;break;
  65. default:_ci->colorspace=OC_CS_UNSPECIFIED;break;
  66. }
  67. switch(_info->pixel_fmt){
  68. case TH_PF_420:_ci->pixelformat=OC_PF_420;break;
  69. case TH_PF_422:_ci->pixelformat=OC_PF_422;break;
  70. case TH_PF_444:_ci->pixelformat=OC_PF_444;break;
  71. default:_ci->pixelformat=OC_PF_RSVD;
  72. }
  73. _ci->target_bitrate=_info->target_bitrate;
  74. _ci->quality=_info->quality;
  75. _ci->keyframe_frequency_force=1<<_info->keyframe_granule_shift;
  76. }
  77. int theora_decode_init(theora_state *_td,theora_info *_ci){
  78. th_api_info *apiinfo;
  79. th_api_wrapper *api;
  80. th_info info;
  81. api=(th_api_wrapper *)_ci->codec_setup;
  82. /*Allocate our own combined API wrapper/theora_info struct.
  83. We put them both in one malloc'd block so that when the API wrapper is
  84. freed, the info struct goes with it.
  85. This avoids having to figure out whether or not we need to free the info
  86. struct in either theora_info_clear() or theora_clear().*/
  87. apiinfo=(th_api_info *)_ogg_calloc(1,sizeof(*apiinfo));
  88. if(apiinfo==NULL)return OC_FAULT;
  89. /*Make our own copy of the info struct, since its lifetime should be
  90. independent of the one we were passed in.*/
  91. *&apiinfo->info=*_ci;
  92. /*Convert the info struct now instead of saving the the one we decoded with
  93. theora_decode_header(), since the user might have modified values (i.e.,
  94. color space, aspect ratio, etc. can be specified from a higher level).
  95. The user also might be doing something "clever" with the header packets if
  96. they are not using an Ogg encapsulation.*/
  97. oc_theora_info2th_info(&info,_ci);
  98. /*Don't bother to copy the setup info; th_decode_alloc() makes its own copy
  99. of the stuff it needs.*/
  100. apiinfo->api.decode=th_decode_alloc(&info,api->setup);
  101. if(apiinfo->api.decode==NULL){
  102. _ogg_free(apiinfo);
  103. return OC_EINVAL;
  104. }
  105. apiinfo->api.clear=(oc_setup_clear_func)th_dec_api_clear;
  106. _td->internal_encode=NULL;
  107. /*Provide entry points for ABI compatibility with old decoder shared libs.*/
  108. _td->internal_decode=(void *)&OC_DEC_DISPATCH_VTBL;
  109. _td->granulepos=0;
  110. _td->i=&apiinfo->info;
  111. _td->i->codec_setup=&apiinfo->api;
  112. return 0;
  113. }
  114. int theora_decode_header(theora_info *_ci,theora_comment *_cc,ogg_packet *_op){
  115. th_api_wrapper *api;
  116. th_info info;
  117. int ret;
  118. api=(th_api_wrapper *)_ci->codec_setup;
  119. /*Allocate an API wrapper struct on demand, since it will not also include a
  120. theora_info struct like the ones that are used in a theora_state struct.*/
  121. if(api==NULL){
  122. _ci->codec_setup=_ogg_calloc(1,sizeof(*api));
  123. if(_ci->codec_setup==NULL)return OC_FAULT;
  124. api=(th_api_wrapper *)_ci->codec_setup;
  125. api->clear=(oc_setup_clear_func)th_dec_api_clear;
  126. }
  127. /*Convert from the theora_info struct instead of saving our own th_info
  128. struct between calls.
  129. The user might be doing something "clever" with the header packets if they
  130. are not using an Ogg encapsulation, and we don't want to break this.*/
  131. oc_theora_info2th_info(&info,_ci);
  132. /*We rely on the fact that theora_comment and th_comment structures are
  133. actually identical.
  134. Take care not to change this fact unless you change the code here as
  135. well!*/
  136. ret=th_decode_headerin(&info,(th_comment *)_cc,&api->setup,_op);
  137. /*We also rely on the fact that the error return code values are the same,
  138. and that the implementations of these two functions return the same set of
  139. them.
  140. Note that theora_decode_header() really can return OC_NOTFORMAT, even
  141. though it is not currently documented to do so.*/
  142. if(ret<0)return ret;
  143. th_info2theora_info(_ci,&info);
  144. return 0;
  145. }
  146. int theora_decode_packetin(theora_state *_td,ogg_packet *_op){
  147. th_api_wrapper *api;
  148. ogg_int64_t gp;
  149. int ret;
  150. if(!_td||!_td->i||!_td->i->codec_setup)return OC_FAULT;
  151. api=(th_api_wrapper *)_td->i->codec_setup;
  152. ret=th_decode_packetin(api->decode,_op,&gp);
  153. if(ret<0)return OC_BADPACKET;
  154. _td->granulepos=gp;
  155. return 0;
  156. }
  157. int theora_decode_YUVout(theora_state *_td,yuv_buffer *_yuv){
  158. th_api_wrapper *api;
  159. th_dec_ctx *decode;
  160. th_ycbcr_buffer buf;
  161. int ret;
  162. if(!_td||!_td->i||!_td->i->codec_setup)return OC_FAULT;
  163. api=(th_api_wrapper *)_td->i->codec_setup;
  164. decode=(th_dec_ctx *)api->decode;
  165. if(!decode)return OC_FAULT;
  166. ret=th_decode_ycbcr_out(decode,buf);
  167. if(ret>=0){
  168. _yuv->y_width=buf[0].width;
  169. _yuv->y_height=buf[0].height;
  170. _yuv->y_stride=buf[0].stride;
  171. _yuv->uv_width=buf[1].width;
  172. _yuv->uv_height=buf[1].height;
  173. _yuv->uv_stride=buf[1].stride;
  174. _yuv->y=buf[0].data;
  175. _yuv->u=buf[1].data;
  176. _yuv->v=buf[2].data;
  177. }
  178. return ret;
  179. }