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.

361 lines
10KB

  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. if(k >= FFMIN(a->format_count, b->format_count)){
  53. av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n");
  54. av_free(ret->formats);
  55. av_free(ret);
  56. return NULL;
  57. }
  58. ret->formats[k++] = a->formats[i];
  59. }
  60. ret->format_count = k;
  61. /* check that there was at least one common format */
  62. if (!ret->format_count) {
  63. av_free(ret->formats);
  64. av_free(ret);
  65. return NULL;
  66. }
  67. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  68. merge_ref(ret, a);
  69. merge_ref(ret, b);
  70. return ret;
  71. }
  72. int ff_fmt_is_in(int fmt, const int *fmts)
  73. {
  74. const int *p;
  75. for (p = fmts; *p != -1; p++) {
  76. if (fmt == *p)
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. #define COPY_INT_LIST(list_copy, list, type) { \
  82. int count = 0; \
  83. if (list) \
  84. for (count = 0; list[count] != -1; count++) \
  85. ; \
  86. list_copy = av_calloc(count+1, sizeof(type)); \
  87. if (list_copy) { \
  88. memcpy(list_copy, list, sizeof(type) * count); \
  89. list_copy[count] = -1; \
  90. } \
  91. }
  92. int *ff_copy_int_list(const int * const list)
  93. {
  94. int *ret = NULL;
  95. COPY_INT_LIST(ret, list, int);
  96. return ret;
  97. }
  98. int64_t *ff_copy_int64_list(const int64_t * const list)
  99. {
  100. int64_t *ret = NULL;
  101. COPY_INT_LIST(ret, list, int64_t);
  102. return ret;
  103. }
  104. #define MAKE_FORMAT_LIST() \
  105. AVFilterFormats *formats; \
  106. int count = 0; \
  107. if (fmts) \
  108. for (count = 0; fmts[count] != -1; count++) \
  109. ; \
  110. formats = av_mallocz(sizeof(AVFilterFormats)); \
  111. if (!formats) return NULL; \
  112. formats->format_count = count; \
  113. if (count) { \
  114. formats->formats = av_malloc(sizeof(*formats->formats)*count); \
  115. if (!formats->formats) { \
  116. av_free(formats); \
  117. return NULL; \
  118. } \
  119. }
  120. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  121. {
  122. MAKE_FORMAT_LIST();
  123. while (count--)
  124. formats->formats[count] = fmts[count];
  125. return formats;
  126. }
  127. AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
  128. {
  129. MAKE_FORMAT_LIST();
  130. if (count)
  131. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  132. return formats;
  133. }
  134. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
  135. {
  136. int64_t *fmts;
  137. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  138. return AVERROR(ENOMEM);
  139. fmts = av_realloc((*avff)->formats,
  140. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  141. if (!fmts)
  142. return AVERROR(ENOMEM);
  143. (*avff)->formats = fmts;
  144. (*avff)->formats[(*avff)->format_count++] = fmt;
  145. return 0;
  146. }
  147. #if FF_API_OLD_ALL_FORMATS_API
  148. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  149. {
  150. return avfilter_make_all_formats(type);
  151. }
  152. #endif
  153. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  154. {
  155. AVFilterFormats *ret = NULL;
  156. int fmt;
  157. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  158. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  159. for (fmt = 0; fmt < num_formats; fmt++)
  160. if ((type != AVMEDIA_TYPE_VIDEO) ||
  161. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  162. avfilter_add_format(&ret, fmt);
  163. return ret;
  164. }
  165. const int64_t avfilter_all_channel_layouts[] = {
  166. #include "all_channel_layouts.h"
  167. -1
  168. };
  169. AVFilterFormats *avfilter_make_all_channel_layouts(void)
  170. {
  171. return avfilter_make_format64_list(avfilter_all_channel_layouts);
  172. }
  173. AVFilterFormats *avfilter_make_all_packing_formats(void)
  174. {
  175. static const int packing[] = {
  176. AVFILTER_PACKED,
  177. AVFILTER_PLANAR,
  178. -1,
  179. };
  180. return avfilter_make_format_list(packing);
  181. }
  182. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  183. {
  184. *ref = f;
  185. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  186. f->refs[f->refcount-1] = ref;
  187. }
  188. static int find_ref_index(AVFilterFormats **ref)
  189. {
  190. int i;
  191. for (i = 0; i < (*ref)->refcount; i++)
  192. if ((*ref)->refs[i] == ref)
  193. return i;
  194. return -1;
  195. }
  196. void avfilter_formats_unref(AVFilterFormats **ref)
  197. {
  198. int idx;
  199. if (!*ref)
  200. return;
  201. idx = find_ref_index(ref);
  202. if (idx >= 0)
  203. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  204. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  205. if (!--(*ref)->refcount) {
  206. av_free((*ref)->formats);
  207. av_free((*ref)->refs);
  208. av_free(*ref);
  209. }
  210. *ref = NULL;
  211. }
  212. void avfilter_formats_changeref(AVFilterFormats **oldref,
  213. AVFilterFormats **newref)
  214. {
  215. int idx = find_ref_index(oldref);
  216. if (idx >= 0) {
  217. (*oldref)->refs[idx] = newref;
  218. *newref = *oldref;
  219. *oldref = NULL;
  220. }
  221. }
  222. /* internal functions for parsing audio format arguments */
  223. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  224. {
  225. char *tail;
  226. int pix_fmt = av_get_pix_fmt(arg);
  227. if (pix_fmt == PIX_FMT_NONE) {
  228. pix_fmt = strtol(arg, &tail, 0);
  229. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  230. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  231. return AVERROR(EINVAL);
  232. }
  233. }
  234. *ret = pix_fmt;
  235. return 0;
  236. }
  237. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  238. {
  239. char *tail;
  240. int sfmt = av_get_sample_fmt(arg);
  241. if (sfmt == AV_SAMPLE_FMT_NONE) {
  242. sfmt = strtol(arg, &tail, 0);
  243. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  244. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  245. return AVERROR(EINVAL);
  246. }
  247. }
  248. *ret = sfmt;
  249. return 0;
  250. }
  251. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  252. {
  253. char *tail;
  254. double srate = av_strtod(arg, &tail);
  255. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  256. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  257. return AVERROR(EINVAL);
  258. }
  259. *ret = srate;
  260. return 0;
  261. }
  262. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  263. {
  264. char *tail;
  265. int64_t chlayout = av_get_channel_layout(arg);
  266. if (chlayout == 0) {
  267. chlayout = strtol(arg, &tail, 10);
  268. if (*tail || chlayout == 0) {
  269. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  270. return AVERROR(EINVAL);
  271. }
  272. }
  273. *ret = chlayout;
  274. return 0;
  275. }
  276. int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
  277. {
  278. char *tail;
  279. int planar = strtol(arg, &tail, 10);
  280. if (*tail) {
  281. planar = !strcmp(arg, "packed") ? 0:
  282. !strcmp(arg, "planar") ? 1: -1;
  283. }
  284. if (planar != 0 && planar != 1) {
  285. av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
  286. return AVERROR(EINVAL);
  287. }
  288. *ret = planar;
  289. return 0;
  290. }
  291. #ifdef TEST
  292. #undef printf
  293. int main(void)
  294. {
  295. const int64_t *cl;
  296. char buf[512];
  297. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  298. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  299. printf("%s\n", buf);
  300. }
  301. return 0;
  302. }
  303. #endif