storable_picture.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "global.h"
  2. #include "mbuffer.h"
  3. #include "memalloc.h"
  4. static void alloc_pic_motion(VideoParameters *p_Vid, PicMotionParams *motion, int size_y, int size_x)
  5. {
  6. // TODO: benski> re-use memory just like for image data
  7. seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
  8. if (!active_sps->frame_mbs_only_flag)
  9. {
  10. get_mem3Dref(&(motion->field_references), 4, size_y, size_x);
  11. }
  12. else
  13. {
  14. motion->field_references = 0; // just in case
  15. }
  16. if (motion_cache_dimensions_match(&p_Vid->motion_cache, size_x, size_y))
  17. {
  18. motion->motion[LIST_0]=motion_cache_get(&p_Vid->motion_cache);
  19. motion->motion[LIST_1]=motion_cache_get(&p_Vid->motion_cache);
  20. }
  21. if (!motion->motion[LIST_0])
  22. get_mem2DPicMotion(&(motion->motion[LIST_0]), size_y, size_x);
  23. if (!motion->motion[LIST_1])
  24. get_mem2DPicMotion(&(motion->motion[LIST_1]), size_y, size_x);
  25. motion->mb_field = calloc (size_y * size_x, sizeof(byte));
  26. if (motion->mb_field == NULL)
  27. no_mem_exit("alloc_storable_picture: motion->mb_field");
  28. get_mem2D (&(motion->field_frame), size_y, size_x);
  29. }
  30. void free_pic_motion(VideoParameters *p_Vid, PicMotionParams *motion, int size_x, int size_y)
  31. {
  32. if (motion->motion[LIST_0])
  33. {
  34. if (motion_cache_dimensions_match(&p_Vid->motion_cache, size_x / BLOCK_SIZE, size_y / BLOCK_SIZE))
  35. {
  36. motion_cache_add(&p_Vid->motion_cache,motion->motion[LIST_0]);
  37. motion_cache_add(&p_Vid->motion_cache,motion->motion[LIST_1]);
  38. }
  39. else
  40. {
  41. free_mem2DPicMotion(motion->motion[LIST_0]);
  42. free_mem2DPicMotion(motion->motion[LIST_1]);
  43. }
  44. motion->motion[LIST_0] = NULL;
  45. motion->motion[LIST_1] = NULL;
  46. }
  47. if (motion->field_references)
  48. {
  49. free_mem3Dref(motion->field_references);
  50. motion->field_references=0;
  51. }
  52. if (motion->mb_field)
  53. {
  54. free(motion->mb_field);
  55. motion->mb_field = NULL;
  56. }
  57. if (motion->field_frame)
  58. {
  59. free_mem2D (motion->field_frame);
  60. motion->field_frame=NULL;
  61. }
  62. }
  63. /*!
  64. ************************************************************************
  65. * \brief
  66. * Free picture memory.
  67. *
  68. * \param p_Vid
  69. * image decoding parameters for current picture
  70. * \param p
  71. * Picture to be freed
  72. *
  73. ************************************************************************
  74. */
  75. static void internal_free_storable_picture(VideoParameters *p_Vid, StorablePicture* p)
  76. {
  77. int nplane;
  78. if (p)
  79. {
  80. free_pic_motion(p_Vid, &p->motion, p->size_x, p->size_y);
  81. //if( IS_INDEPENDENT(p_Vid) )
  82. {
  83. for( nplane=0; nplane<MAX_PLANE; nplane++ )
  84. {
  85. free_pic_motion(p_Vid, &p->JVmotion[nplane], p->size_x, p->size_y);
  86. }
  87. }
  88. if (image_cache_dimensions_match(&p_Vid->image_cache[0], p->size_x, p->size_y))
  89. image_cache_add(&p_Vid->image_cache[0], p->imgY);
  90. else
  91. free_memImage(p->imgY);
  92. if (image_cache_dimensions_match(&p_Vid->image_cache[1], p->size_x_cr, p->size_y_cr))
  93. image_cache_add(&p_Vid->image_cache[1], p->imgUV[0]);
  94. else
  95. free_memImage(p->imgUV[0]);
  96. if (image_cache_dimensions_match(&p_Vid->image_cache[1], p->size_x_cr, p->size_y_cr))
  97. image_cache_add(&p_Vid->image_cache[1], p->imgUV[1]);
  98. else
  99. free_memImage(p->imgUV[1]);
  100. if (p->slice_id)
  101. {
  102. free_mem2Dshort(p->slice_id);
  103. p->slice_id=NULL;
  104. }
  105. if (p->seiHasTone_mapping)
  106. free(p->tone_mapping_lut);
  107. _aligned_free(p);
  108. p = NULL;
  109. }
  110. }
  111. void free_storable_picture(VideoParameters *p_Vid, StorablePicture* p)
  112. {
  113. if (p && --p->retain_count == 0)
  114. {
  115. internal_free_storable_picture(p_Vid, p);
  116. }
  117. }
  118. /*!
  119. ************************************************************************
  120. * \brief
  121. * Allocate memory for a stored picture.
  122. *
  123. * \param p_Vid
  124. * image decoding parameters for current picture
  125. * \param structure
  126. * picture structure
  127. * \param size_x
  128. * horizontal luma size
  129. * \param size_y
  130. * vertical luma size
  131. * \param size_x_cr
  132. * horizontal chroma size
  133. * \param size_y_cr
  134. * vertical chroma size
  135. *
  136. * \return
  137. * the allocated StorablePicture structure
  138. ************************************************************************
  139. */
  140. #define ROUNDUP32(size) (((size)+31) & ~31)
  141. StorablePicture* alloc_storable_picture(VideoParameters *p_Vid, PictureStructure structure, int size_x, int size_y, int size_x_cr, int size_y_cr)
  142. {
  143. seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
  144. StorablePicture *s;
  145. int nplane;
  146. //printf ("Allocating (%s) picture (x=%d, y=%d, x_cr=%d, y_cr=%d)\n", (type == FRAME)?"FRAME":(type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", size_x, size_y, size_x_cr, size_y_cr);
  147. s = _aligned_malloc(sizeof(StorablePicture), 32);
  148. if (NULL==s)
  149. return 0;
  150. memset(s, 0, sizeof(StorablePicture));
  151. s->retain_count = 1;
  152. s->time_code = (uint64_t)-666;
  153. if (structure!=FRAME)
  154. {
  155. size_y /= 2;
  156. size_y_cr /= 2;
  157. }
  158. s->PicSizeInMbs = (size_x*size_y)/256;
  159. if (image_cache_dimensions_match(&p_Vid->image_cache[0], size_x, size_y))
  160. s->imgY = image_cache_get(&p_Vid->image_cache[0]);
  161. if (!s->imgY)
  162. s->imgY = get_memImage(size_x, size_y);
  163. if (active_sps->chroma_format_idc != YUV400)
  164. {
  165. if (image_cache_dimensions_match(&p_Vid->image_cache[1], size_x_cr, size_y_cr))
  166. {
  167. s->imgUV[0] = image_cache_get(&p_Vid->image_cache[1]);
  168. s->imgUV[1] = image_cache_get(&p_Vid->image_cache[1]);
  169. }
  170. if (!s->imgUV[0])
  171. s->imgUV[0] = get_memImage(size_x_cr, size_y);
  172. if (!s->imgUV[1])
  173. s->imgUV[1] = get_memImage(size_x_cr, size_y);
  174. }
  175. get_mem2Dshort (&(s->slice_id), size_y / MB_BLOCK_SIZE, size_x / MB_BLOCK_SIZE);
  176. alloc_pic_motion(p_Vid, &s->motion, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  177. if( IS_INDEPENDENT(p_Vid) )
  178. {
  179. for( nplane=0; nplane<MAX_PLANE; nplane++ )
  180. {
  181. alloc_pic_motion(p_Vid, &s->JVmotion[nplane], size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  182. }
  183. }
  184. s->structure=structure;
  185. s->size_x = size_x;
  186. s->size_y = size_y;
  187. s->size_x_cr = size_x_cr;
  188. s->size_y_cr = size_y_cr;
  189. s->size_x_m1 = size_x - 1;
  190. s->size_y_m1 = size_y - 1;
  191. s->size_x_cr_m1 = size_x_cr - 1;
  192. s->size_y_cr_m1 = size_y_cr - 1;
  193. s->top_field = p_Vid->no_reference_picture;
  194. s->bottom_field = p_Vid->no_reference_picture;
  195. s->frame = p_Vid->no_reference_picture;
  196. return s;
  197. }
  198. void out_storable_picture_add(VideoParameters *img, StorablePicture *pic)
  199. {
  200. if (img->out_pictures)
  201. {
  202. // see if we're full
  203. if (img->size_out_pictures == img->num_out_pictures)
  204. {
  205. StorablePicture *pic=0;
  206. out_storable_picture_get(img, &pic);
  207. if (pic)
  208. free_storable_picture(img, pic);
  209. }
  210. img->out_pictures[img->num_out_pictures++] = pic;
  211. pic->retain_count++;
  212. }
  213. }
  214. void out_storable_picture_get(VideoParameters *img, StorablePicture **pic)
  215. {
  216. *pic = 0;
  217. if (img->out_pictures && img->num_out_pictures)
  218. {
  219. *pic = img->out_pictures[0];
  220. img->num_out_pictures--;
  221. memmove(img->out_pictures, &img->out_pictures[1], img->num_out_pictures * sizeof(StorablePicture *));
  222. }
  223. }
  224. void out_storable_pictures_init(VideoParameters *img, size_t count)
  225. {
  226. img->out_pictures = (StorablePicture **)calloc(sizeof(StorablePicture *), count);
  227. img->size_out_pictures = count;
  228. img->num_out_pictures = 0;
  229. }
  230. void out_storable_pictures_destroy(VideoParameters *img)
  231. {
  232. size_t i=0;
  233. while (img->num_out_pictures)
  234. {
  235. StorablePicture *pic=0;
  236. out_storable_picture_get(img, &pic);
  237. if (pic)
  238. free_storable_picture(img, pic);
  239. }
  240. free(img->out_pictures);
  241. img->out_pictures = 0;
  242. img->size_out_pictures = 0;
  243. }