formats.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #if FF_API_OLD_ALL_FORMATS_API
  118. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  119. {
  120. return avfilter_make_all_formats(type);
  121. }
  122. #endif
  123. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
  124. {
  125. AVFilterFormats *ret = NULL;
  126. int fmt;
  127. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  128. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  129. for (fmt = 0; fmt < num_formats; fmt++)
  130. if ((type != AVMEDIA_TYPE_VIDEO) ||
  131. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  132. avfilter_add_format(&ret, fmt);
  133. return ret;
  134. }
  135. const int64_t avfilter_all_channel_layouts[] = {
  136. #include "all_channel_layouts.h"
  137. -1
  138. };
  139. AVFilterFormats *avfilter_make_all_channel_layouts(void)
  140. {
  141. return avfilter_make_format64_list(avfilter_all_channel_layouts);
  142. }
  143. AVFilterFormats *avfilter_make_all_packing_formats(void)
  144. {
  145. static int packing[] = {
  146. AVFILTER_PACKED,
  147. AVFILTER_PLANAR,
  148. -1,
  149. };
  150. return avfilter_make_format_list(packing);
  151. }
  152. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  153. {
  154. *ref = f;
  155. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  156. f->refs[f->refcount-1] = ref;
  157. }
  158. static int find_ref_index(AVFilterFormats **ref)
  159. {
  160. int i;
  161. for (i = 0; i < (*ref)->refcount; i++)
  162. if ((*ref)->refs[i] == ref)
  163. return i;
  164. return -1;
  165. }
  166. void avfilter_formats_unref(AVFilterFormats **ref)
  167. {
  168. int idx;
  169. if (!*ref)
  170. return;
  171. idx = find_ref_index(ref);
  172. if (idx >= 0)
  173. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  174. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  175. if (!--(*ref)->refcount) {
  176. av_free((*ref)->formats);
  177. av_free((*ref)->refs);
  178. av_free(*ref);
  179. }
  180. *ref = NULL;
  181. }
  182. void avfilter_formats_changeref(AVFilterFormats **oldref,
  183. AVFilterFormats **newref)
  184. {
  185. int idx = find_ref_index(oldref);
  186. if (idx >= 0) {
  187. (*oldref)->refs[idx] = newref;
  188. *newref = *oldref;
  189. *oldref = NULL;
  190. }
  191. }
  192. /* internal functions for parsing audio format arguments */
  193. int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
  194. {
  195. char *tail;
  196. int pix_fmt = av_get_pix_fmt(arg);
  197. if (pix_fmt == PIX_FMT_NONE) {
  198. pix_fmt = strtol(arg, &tail, 0);
  199. if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
  200. av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
  201. return AVERROR(EINVAL);
  202. }
  203. }
  204. *ret = pix_fmt;
  205. return 0;
  206. }
  207. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
  208. {
  209. char *tail;
  210. int sfmt = av_get_sample_fmt(arg);
  211. if (sfmt == AV_SAMPLE_FMT_NONE) {
  212. sfmt = strtol(arg, &tail, 0);
  213. if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
  214. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
  215. return AVERROR(EINVAL);
  216. }
  217. }
  218. *ret = sfmt;
  219. return 0;
  220. }
  221. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
  222. {
  223. char *tail;
  224. double srate = av_strtod(arg, &tail);
  225. if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
  226. av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
  227. return AVERROR(EINVAL);
  228. }
  229. *ret = srate;
  230. return 0;
  231. }
  232. int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
  233. {
  234. char *tail;
  235. int64_t chlayout = av_get_channel_layout(arg);
  236. if (chlayout == 0) {
  237. chlayout = strtol(arg, &tail, 10);
  238. if (*tail || chlayout == 0) {
  239. av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
  240. return AVERROR(EINVAL);
  241. }
  242. }
  243. *ret = chlayout;
  244. return 0;
  245. }
  246. int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
  247. {
  248. char *tail;
  249. int planar = strtol(arg, &tail, 10);
  250. if (*tail) {
  251. planar = !strcmp(arg, "packed") ? 0:
  252. !strcmp(arg, "planar") ? 1: -1;
  253. }
  254. if (planar != 0 && planar != 1) {
  255. av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
  256. return AVERROR(EINVAL);
  257. }
  258. *ret = planar;
  259. return 0;
  260. }
  261. #ifdef TEST
  262. #undef printf
  263. int main(void)
  264. {
  265. const int64_t *cl;
  266. char buf[512];
  267. for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
  268. av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
  269. printf("%s\n", buf);
  270. }
  271. return 0;
  272. }
  273. #endif