formats.c 10 KB

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