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.

222 lines
6.5KB

  1. /*
  2. * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "libavutil/mem.h"
  26. static const SchroVideoFormatInfo ff_schro_video_format_info[] = {
  27. { 640, 480, 24000, 1001},
  28. { 176, 120, 15000, 1001},
  29. { 176, 144, 25, 2 },
  30. { 352, 240, 15000, 1001},
  31. { 352, 288, 25, 2 },
  32. { 704, 480, 15000, 1001},
  33. { 704, 576, 25, 2 },
  34. { 720, 480, 30000, 1001},
  35. { 720, 576, 25, 1 },
  36. { 1280, 720, 60000, 1001},
  37. { 1280, 720, 50, 1 },
  38. { 1920, 1080, 30000, 1001},
  39. { 1920, 1080, 25, 1 },
  40. { 1920, 1080, 60000, 1001},
  41. { 1920, 1080, 50, 1 },
  42. { 2048, 1080, 24, 1 },
  43. { 4096, 2160, 24, 1 },
  44. };
  45. static unsigned int get_video_format_idx(AVCodecContext *avccontext)
  46. {
  47. unsigned int ret_idx = 0;
  48. unsigned int idx;
  49. unsigned int num_formats = sizeof(ff_schro_video_format_info) /
  50. sizeof(ff_schro_video_format_info[0]);
  51. for (idx = 1; idx < num_formats; ++idx) {
  52. const SchroVideoFormatInfo *vf = &ff_schro_video_format_info[idx];
  53. if (avccontext->width == vf->width &&
  54. avccontext->height == vf->height) {
  55. ret_idx = idx;
  56. if (avccontext->time_base.den == vf->frame_rate_num &&
  57. avccontext->time_base.num == vf->frame_rate_denom)
  58. return idx;
  59. }
  60. }
  61. return ret_idx;
  62. }
  63. void ff_schro_queue_init(FFSchroQueue *queue)
  64. {
  65. queue->p_head = queue->p_tail = NULL;
  66. queue->size = 0;
  67. }
  68. void ff_schro_queue_free(FFSchroQueue *queue, void (*free_func)(void *))
  69. {
  70. while (queue->p_head)
  71. free_func(ff_schro_queue_pop(queue));
  72. }
  73. int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data)
  74. {
  75. FFSchroQueueElement *p_new = av_mallocz(sizeof(FFSchroQueueElement));
  76. if (!p_new)
  77. return -1;
  78. p_new->data = p_data;
  79. if (!queue->p_head)
  80. queue->p_head = p_new;
  81. else
  82. queue->p_tail->next = p_new;
  83. queue->p_tail = p_new;
  84. ++queue->size;
  85. return 0;
  86. }
  87. void *ff_schro_queue_pop(FFSchroQueue *queue)
  88. {
  89. FFSchroQueueElement *top = queue->p_head;
  90. if (top) {
  91. void *data = top->data;
  92. queue->p_head = queue->p_head->next;
  93. --queue->size;
  94. av_freep(&top);
  95. return data;
  96. }
  97. return NULL;
  98. }
  99. /**
  100. * Schroedinger video preset table. Ensure that this tables matches up correctly
  101. * with the ff_schro_video_format_info table.
  102. */
  103. static const SchroVideoFormatEnum ff_schro_video_formats[]={
  104. SCHRO_VIDEO_FORMAT_CUSTOM ,
  105. SCHRO_VIDEO_FORMAT_QSIF ,
  106. SCHRO_VIDEO_FORMAT_QCIF ,
  107. SCHRO_VIDEO_FORMAT_SIF ,
  108. SCHRO_VIDEO_FORMAT_CIF ,
  109. SCHRO_VIDEO_FORMAT_4SIF ,
  110. SCHRO_VIDEO_FORMAT_4CIF ,
  111. SCHRO_VIDEO_FORMAT_SD480I_60 ,
  112. SCHRO_VIDEO_FORMAT_SD576I_50 ,
  113. SCHRO_VIDEO_FORMAT_HD720P_60 ,
  114. SCHRO_VIDEO_FORMAT_HD720P_50 ,
  115. SCHRO_VIDEO_FORMAT_HD1080I_60 ,
  116. SCHRO_VIDEO_FORMAT_HD1080I_50 ,
  117. SCHRO_VIDEO_FORMAT_HD1080P_60 ,
  118. SCHRO_VIDEO_FORMAT_HD1080P_50 ,
  119. SCHRO_VIDEO_FORMAT_DC2K_24 ,
  120. SCHRO_VIDEO_FORMAT_DC4K_24 ,
  121. };
  122. SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
  123. {
  124. unsigned int num_formats = sizeof(ff_schro_video_formats) /
  125. sizeof(ff_schro_video_formats[0]);
  126. unsigned int idx = get_video_format_idx(avccontext);
  127. return (idx < num_formats) ? ff_schro_video_formats[idx] :
  128. SCHRO_VIDEO_FORMAT_CUSTOM;
  129. }
  130. int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt,
  131. SchroFrameFormat *schro_frame_fmt)
  132. {
  133. unsigned int num_formats = sizeof(schro_pixel_format_map) /
  134. sizeof(schro_pixel_format_map[0]);
  135. int idx;
  136. for (idx = 0; idx < num_formats; ++idx) {
  137. if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
  138. *schro_frame_fmt = schro_pixel_format_map[idx].schro_frame_fmt;
  139. return 0;
  140. }
  141. }
  142. return -1;
  143. }
  144. static void free_schro_frame(SchroFrame *frame, void *priv)
  145. {
  146. AVPicture *p_pic = priv;
  147. if (!p_pic)
  148. return;
  149. avpicture_free(p_pic);
  150. av_freep(&p_pic);
  151. }
  152. SchroFrame *ff_create_schro_frame(AVCodecContext *avccontext,
  153. SchroFrameFormat schro_frame_fmt)
  154. {
  155. AVPicture *p_pic;
  156. SchroFrame *p_frame;
  157. int y_width, uv_width;
  158. int y_height, uv_height;
  159. int i;
  160. y_width = avccontext->width;
  161. y_height = avccontext->height;
  162. uv_width = y_width >> (SCHRO_FRAME_FORMAT_H_SHIFT(schro_frame_fmt));
  163. uv_height = y_height >> (SCHRO_FRAME_FORMAT_V_SHIFT(schro_frame_fmt));
  164. p_pic = av_mallocz(sizeof(AVPicture));
  165. if (!p_pic || avpicture_alloc(p_pic, avccontext->pix_fmt, y_width, y_height) < 0) {
  166. av_free(p_pic);
  167. return NULL;
  168. }
  169. p_frame = schro_frame_new();
  170. p_frame->format = schro_frame_fmt;
  171. p_frame->width = y_width;
  172. p_frame->height = y_height;
  173. schro_frame_set_free_callback(p_frame, free_schro_frame, (void *)p_pic);
  174. for (i = 0; i < 3; ++i) {
  175. p_frame->components[i].width = i ? uv_width : y_width;
  176. p_frame->components[i].stride = p_pic->linesize[i];
  177. p_frame->components[i].height = i ? uv_height : y_height;
  178. p_frame->components[i].length =
  179. p_frame->components[i].stride * p_frame->components[i].height;
  180. p_frame->components[i].data = p_pic->data[i];
  181. if (i) {
  182. p_frame->components[i].v_shift =
  183. SCHRO_FRAME_FORMAT_V_SHIFT(p_frame->format);
  184. p_frame->components[i].h_shift =
  185. SCHRO_FRAME_FORMAT_H_SHIFT(p_frame->format);
  186. }
  187. }
  188. return p_frame;
  189. }