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.

362 lines
10.0KB

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