formats.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/pixdesc.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. /**
  25. * Add all refs from a to ret and destroy a.
  26. */
  27. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  28. {
  29. int i;
  30. for(i = 0; i < a->refcount; i ++) {
  31. ret->refs[ret->refcount] = a->refs[i];
  32. *ret->refs[ret->refcount++] = ret;
  33. }
  34. av_free(a->refs);
  35. av_free(a->formats);
  36. av_free(a);
  37. }
  38. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  39. {
  40. AVFilterFormats *ret;
  41. unsigned i, j, k = 0;
  42. ret = av_mallocz(sizeof(AVFilterFormats));
  43. /* merge list of formats */
  44. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  45. b->format_count));
  46. for(i = 0; i < a->format_count; i ++)
  47. for(j = 0; j < b->format_count; j ++)
  48. if(a->formats[i] == b->formats[j])
  49. ret->formats[k++] = a->formats[i];
  50. ret->format_count = k;
  51. /* check that there was at least one common format */
  52. if(!ret->format_count) {
  53. av_free(ret->formats);
  54. av_free(ret);
  55. return NULL;
  56. }
  57. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  58. merge_ref(ret, a);
  59. merge_ref(ret, b);
  60. return ret;
  61. }
  62. int ff_fmt_is_in(int fmt, const int *fmts)
  63. {
  64. const int *p;
  65. for (p = fmts; *p != PIX_FMT_NONE; p++) {
  66. if (fmt == *p)
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  72. {
  73. AVFilterFormats *formats;
  74. int count;
  75. for (count = 0; fmts[count] != -1; count++)
  76. ;
  77. formats = av_mallocz(sizeof(AVFilterFormats));
  78. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  79. formats->format_count = count;
  80. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  81. return formats;
  82. }
  83. int avfilter_add_format(AVFilterFormats **avff, int fmt)
  84. {
  85. int *fmts;
  86. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  87. return AVERROR(ENOMEM);
  88. fmts = av_realloc((*avff)->formats,
  89. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  90. if (!fmts)
  91. return AVERROR(ENOMEM);
  92. (*avff)->formats = fmts;
  93. (*avff)->formats[(*avff)->format_count++] = fmt;
  94. return 0;
  95. }
  96. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  97. {
  98. AVFilterFormats *ret = NULL;
  99. int fmt;
  100. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  101. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  102. for (fmt = 0; fmt < num_formats; fmt++)
  103. if ((type != AVMEDIA_TYPE_VIDEO) ||
  104. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  105. avfilter_add_format(&ret, fmt);
  106. return ret;
  107. }
  108. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  109. {
  110. *ref = f;
  111. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  112. f->refs[f->refcount-1] = ref;
  113. }
  114. static int find_ref_index(AVFilterFormats **ref)
  115. {
  116. int i;
  117. for(i = 0; i < (*ref)->refcount; i ++)
  118. if((*ref)->refs[i] == ref)
  119. return i;
  120. return -1;
  121. }
  122. void avfilter_formats_unref(AVFilterFormats **ref)
  123. {
  124. int idx;
  125. if (!*ref)
  126. return;
  127. idx = find_ref_index(ref);
  128. if(idx >= 0)
  129. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  130. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  131. if(!--(*ref)->refcount) {
  132. av_free((*ref)->formats);
  133. av_free((*ref)->refs);
  134. av_free(*ref);
  135. }
  136. *ref = NULL;
  137. }
  138. void avfilter_formats_changeref(AVFilterFormats **oldref,
  139. AVFilterFormats **newref)
  140. {
  141. int idx = find_ref_index(oldref);
  142. if(idx >= 0) {
  143. (*oldref)->refs[idx] = newref;
  144. *newref = *oldref;
  145. *oldref = NULL;
  146. }
  147. }