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.

313 lines
8.6KB

  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/eval.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/audioconvert.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. /**
  27. * Add all refs from a to ret and destroy a.
  28. */
  29. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  30. {
  31. int i;
  32. for (i = 0; i < a->refcount; i++) {
  33. ret->refs[ret->refcount] = a->refs[i];
  34. *ret->refs[ret->refcount++] = ret;
  35. }
  36. av_free(a->refs);
  37. av_free(a->formats);
  38. av_free(a);
  39. }
  40. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  41. {
  42. AVFilterFormats *ret;
  43. unsigned i, j, k = 0;
  44. if (a == b) return a;
  45. ret = av_mallocz(sizeof(AVFilterFormats));
  46. /* merge list of formats */
  47. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  48. b->format_count));
  49. for (i = 0; i < a->format_count; i++)
  50. for (j = 0; j < b->format_count; j++)
  51. if (a->formats[i] == b->formats[j])
  52. ret->formats[k++] = a->formats[i];
  53. ret->format_count = k;
  54. /* check that there was at least one common format */
  55. if (!ret->format_count) {
  56. av_free(ret->formats);
  57. av_free(ret);
  58. return NULL;
  59. }
  60. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  61. merge_ref(ret, a);
  62. merge_ref(ret, b);
  63. return ret;
  64. }
  65. int ff_fmt_is_in(int fmt, const int *fmts)
  66. {
  67. const int *p;
  68. for (p = fmts; *p != -1; p++) {
  69. if (fmt == *p)
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. #define MAKE_FORMAT_LIST() \
  75. AVFilterFormats *formats; \
  76. int count = 0; \
  77. if (fmts) \
  78. for (count = 0; fmts[count] != -1; count++) \
  79. ; \
  80. formats = av_mallocz(sizeof(AVFilterFormats)); \
  81. if (!formats) return NULL; \
  82. formats->format_count = count; \
  83. if (count) { \
  84. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  85. if (!formats->formats) { \
  86. av_free(formats); \
  87. return NULL; \
  88. } \
  89. }
  90. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  91. {
  92. MAKE_FORMAT_LIST();
  93. while (count--)
  94. formats->formats[count] = fmts[count];
  95. return formats;
  96. }
  97. AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
  98. {
  99. MAKE_FORMAT_LIST();
  100. if (count)
  101. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  102. return formats;
  103. }
  104. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  105. {
  106. int64_t *fmts;
  107. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  108. return AVERROR(ENOMEM);
  109. fmts = av_realloc((*avff)->formats,
  110. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  111. if (!fmts)
  112. return AVERROR(ENOMEM);
  113. (*avff)->formats = fmts;
  114. (*avff)->formats[(*avff)->format_count++] = fmt;
  115. return 0;
  116. }
  117. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  118. {
  119. AVFilterFormats *ret = NULL;
  120. int fmt;
  121. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  122. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  123. for (fmt = 0; fmt < num_formats; fmt++)
  124. if ((type != AVMEDIA_TYPE_VIDEO) ||
  125. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  126. avfilter_add_format(&ret, fmt);
  127. return ret;
  128. }
  129. AVFilterFormats *avfilter_all_channel_layouts(void)
  130. {
  131. static int64_t chlayouts[] = {
  132. AV_CH_LAYOUT_MONO,
  133. AV_CH_LAYOUT_STEREO,
  134. AV_CH_LAYOUT_4POINT0,
  135. AV_CH_LAYOUT_QUAD,
  136. AV_CH_LAYOUT_5POINT0,
  137. AV_CH_LAYOUT_5POINT0_BACK,
  138. AV_CH_LAYOUT_5POINT1,
  139. AV_CH_LAYOUT_5POINT1_BACK,
  140. AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  141. AV_CH_LAYOUT_7POINT1,
  142. AV_CH_LAYOUT_7POINT1_WIDE,
  143. AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
  144. -1,
  145. };
  146. return avfilter_make_format64_list(chlayouts);
  147. }
  148. AVFilterFormats *avfilter_all_packing_formats(void)
  149. {
  150. static int packing[] = {
  151. AVFILTER_PACKED,
  152. AVFILTER_PLANAR,
  153. -1,
  154. };
  155. return avfilter_make_format_list(packing);
  156. }
  157. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  158. {
  159. *ref = f;
  160. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  161. f->refs[f->refcount-1] = ref;
  162. }
  163. static int find_ref_index(AVFilterFormats **ref)
  164. {
  165. int i;
  166. for (i = 0; i < (*ref)->refcount; i++)
  167. if ((*ref)->refs[i] == ref)
  168. return i;
  169. return -1;
  170. }
  171. void avfilter_formats_unref(AVFilterFormats **ref)
  172. {
  173. int idx;
  174. if (!*ref)
  175. return;
  176. idx = find_ref_index(ref);
  177. if (idx >= 0)
  178. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  179. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  180. if (!--(*ref)->refcount) {
  181. av_free((*ref)->formats);
  182. av_free((*ref)->refs);
  183. av_free(*ref);
  184. }
  185. *ref = NULL;
  186. }
  187. void avfilter_formats_changeref(AVFilterFormats **oldref,
  188. AVFilterFormats **newref)
  189. {
  190. int idx = find_ref_index(oldref);
  191. if (idx >= 0) {
  192. (*oldref)->refs[idx] = newref;
  193. *newref = *oldref;
  194. *oldref = NULL;
  195. }
  196. }
  197. /* internal functions for parsing audio format arguments */
  198. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  199. {
  200. char *tail;
  201. int pix_fmt = av_get_pix_fmt(arg);
  202. if (pix_fmt == PIX_FMT_NONE) {
  203. pix_fmt = strtol(arg, &tail, 0);
  204. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  205. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  206. return AVERROR(EINVAL);
  207. }
  208. }
  209. *ret = pix_fmt;
  210. return 0;
  211. }
  212. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  213. {
  214. char *tail;
  215. int sfmt = av_get_sample_fmt(arg);
  216. if (sfmt == AV_SAMPLE_FMT_NONE) {
  217. sfmt = strtol(arg, &tail, 0);
  218. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  219. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  220. return AVERROR(EINVAL);
  221. }
  222. }
  223. *ret = sfmt;
  224. return 0;
  225. }
  226. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  227. {
  228. char *tail;
  229. double srate = av_strtod(arg, &tail);
  230. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  231. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  232. return AVERROR(EINVAL);
  233. }
  234. *ret = srate;
  235. return 0;
  236. }
  237. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  238. {
  239. char *tail;
  240. int64_t chlayout = av_get_channel_layout(arg);
  241. if (chlayout == 0) {
  242. chlayout = strtol(arg, &tail, 10);
  243. if (*tail || chlayout == 0) {
  244. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  245. return AVERROR(EINVAL);
  246. }
  247. }
  248. *ret = chlayout;
  249. return 0;
  250. }
  251. int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
  252. {
  253. char *tail;
  254. int planar = strtol(arg, &tail, 10);
  255. if (*tail) {
  256. planar = !strcmp(arg, "packed") ? 0:
  257. !strcmp(arg, "planar") ? 1: -1;
  258. }
  259. if (planar != 0 && planar != 1) {
  260. av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
  261. return AVERROR(EINVAL);
  262. }
  263. *ret = planar;
  264. return 0;
  265. }