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.

223 lines
6.5KB

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