You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.4KB

  1. /*
  2. * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * function definitions common to libschroedinger decoder and encoder
  23. */
  24. #include "libschroedinger.h"
  25. static const SchroVideoFormatInfo ff_schro_video_format_info[] = {
  26. { 640, 480, 24000, 1001},
  27. { 176, 120, 15000, 1001},
  28. { 176, 144, 25, 2 },
  29. { 352, 240, 15000, 1001},
  30. { 352, 288, 25, 2 },
  31. { 704, 480, 15000, 1001},
  32. { 704, 576, 25, 2 },
  33. { 720, 480, 30000, 1001},
  34. { 720, 576, 25, 1 },
  35. { 1280, 720, 60000, 1001},
  36. { 1280, 720, 50, 1 },
  37. { 1920, 1080, 30000, 1001},
  38. { 1920, 1080, 25, 1 },
  39. { 1920, 1080, 60000, 1001},
  40. { 1920, 1080, 50, 1 },
  41. { 2048, 1080, 24, 1 },
  42. { 4096, 2160, 24, 1 },
  43. };
  44. static unsigned int get_video_format_idx(AVCodecContext *avccontext)
  45. {
  46. unsigned int ret_idx = 0;
  47. unsigned int idx;
  48. unsigned int num_formats = sizeof(ff_schro_video_format_info) /
  49. sizeof(ff_schro_video_format_info[0]);
  50. for (idx = 1; idx < num_formats; ++idx) {
  51. const SchroVideoFormatInfo *vf = &ff_schro_video_format_info[idx];
  52. if (avccontext->width == vf->width &&
  53. avccontext->height == vf->height) {
  54. ret_idx = idx;
  55. if (avccontext->time_base.den == vf->frame_rate_num &&
  56. avccontext->time_base.num == vf->frame_rate_denom)
  57. return idx;
  58. }
  59. }
  60. return ret_idx;
  61. }
  62. void ff_schro_queue_init(FFSchroQueue *queue)
  63. {
  64. queue->p_head = queue->p_tail = NULL;
  65. queue->size = 0;
  66. }
  67. void ff_schro_queue_free(FFSchroQueue *queue, void (*free_func)(void *))
  68. {
  69. while (queue->p_head)
  70. free_func(ff_schro_queue_pop(queue));
  71. }
  72. int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data)
  73. {
  74. FFSchroQueueElement *p_new = av_mallocz(sizeof(FFSchroQueueElement));
  75. if (!p_new)
  76. return -1;
  77. p_new->data = p_data;
  78. if (!queue->p_head)
  79. queue->p_head = p_new;
  80. else
  81. queue->p_tail->next = p_new;
  82. queue->p_tail = p_new;
  83. ++queue->size;
  84. return 0;
  85. }
  86. void *ff_schro_queue_pop(FFSchroQueue *queue)
  87. {
  88. FFSchroQueueElement *top = queue->p_head;
  89. if (top) {
  90. void *data = top->data;
  91. queue->p_head = queue->p_head->next;
  92. --queue->size;
  93. av_freep(&top);
  94. return data;
  95. }
  96. return NULL;
  97. }
  98. /**
  99. * Schroedinger video preset table. Ensure that this tables matches up correctly
  100. * with the ff_schro_video_format_info table.
  101. */
  102. static const SchroVideoFormatEnum ff_schro_video_formats[]={
  103. SCHRO_VIDEO_FORMAT_CUSTOM ,
  104. SCHRO_VIDEO_FORMAT_QSIF ,
  105. SCHRO_VIDEO_FORMAT_QCIF ,
  106. SCHRO_VIDEO_FORMAT_SIF ,
  107. SCHRO_VIDEO_FORMAT_CIF ,
  108. SCHRO_VIDEO_FORMAT_4SIF ,
  109. SCHRO_VIDEO_FORMAT_4CIF ,
  110. SCHRO_VIDEO_FORMAT_SD480I_60 ,
  111. SCHRO_VIDEO_FORMAT_SD576I_50 ,
  112. SCHRO_VIDEO_FORMAT_HD720P_60 ,
  113. SCHRO_VIDEO_FORMAT_HD720P_50 ,
  114. SCHRO_VIDEO_FORMAT_HD1080I_60 ,
  115. SCHRO_VIDEO_FORMAT_HD1080I_50 ,
  116. SCHRO_VIDEO_FORMAT_HD1080P_60 ,
  117. SCHRO_VIDEO_FORMAT_HD1080P_50 ,
  118. SCHRO_VIDEO_FORMAT_DC2K_24 ,
  119. SCHRO_VIDEO_FORMAT_DC4K_24 ,
  120. };
  121. SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
  122. {
  123. unsigned int num_formats = sizeof(ff_schro_video_formats) /
  124. sizeof(ff_schro_video_formats[0]);
  125. unsigned int idx = get_video_format_idx(avccontext);
  126. return (idx < num_formats) ? ff_schro_video_formats[idx] :
  127. SCHRO_VIDEO_FORMAT_CUSTOM;
  128. }
  129. int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt,
  130. SchroFrameFormat *schro_frame_fmt)
  131. {
  132. unsigned int num_formats = sizeof(schro_pixel_format_map) /
  133. sizeof(schro_pixel_format_map[0]);
  134. int idx;
  135. for (idx = 0; idx < num_formats; ++idx) {
  136. if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
  137. *schro_frame_fmt = schro_pixel_format_map[idx].schro_frame_fmt;
  138. return 0;
  139. }
  140. }
  141. return -1;
  142. }
  143. static void free_schro_frame(SchroFrame *frame, void *priv)
  144. {
  145. AVPicture *p_pic = priv;
  146. if (!p_pic)
  147. return;
  148. avpicture_free(p_pic);
  149. av_freep(&p_pic);
  150. }
  151. SchroFrame *ff_create_schro_frame(AVCodecContext *avccontext,
  152. SchroFrameFormat schro_frame_fmt)
  153. {
  154. AVPicture *p_pic;
  155. SchroFrame *p_frame;
  156. int y_width, uv_width;
  157. int y_height, uv_height;
  158. int i;
  159. y_width = avccontext->width;
  160. y_height = avccontext->height;
  161. uv_width = y_width >> (SCHRO_FRAME_FORMAT_H_SHIFT(schro_frame_fmt));
  162. uv_height = y_height >> (SCHRO_FRAME_FORMAT_V_SHIFT(schro_frame_fmt));
  163. p_pic = av_mallocz(sizeof(AVPicture));
  164. avpicture_alloc(p_pic, avccontext->pix_fmt, y_width, y_height);
  165. p_frame = schro_frame_new();
  166. p_frame->format = schro_frame_fmt;
  167. p_frame->width = y_width;
  168. p_frame->height = y_height;
  169. schro_frame_set_free_callback(p_frame, free_schro_frame, (void *)p_pic);
  170. for (i = 0; i < 3; ++i) {
  171. p_frame->components[i].width = i ? uv_width : y_width;
  172. p_frame->components[i].stride = p_pic->linesize[i];
  173. p_frame->components[i].height = i ? uv_height : y_height;
  174. p_frame->components[i].length =
  175. p_frame->components[i].stride * p_frame->components[i].height;
  176. p_frame->components[i].data = p_pic->data[i];
  177. if (i) {
  178. p_frame->components[i].v_shift =
  179. SCHRO_FRAME_FORMAT_V_SHIFT(p_frame->format);
  180. p_frame->components[i].h_shift =
  181. SCHRO_FRAME_FORMAT_H_SHIFT(p_frame->format);
  182. }
  183. }
  184. return p_frame;
  185. }