formats.c 9.0 KB

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