formats.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "avfilter.h"
  22. /**
  23. * Add all refs from a to ret and destroy a.
  24. */
  25. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  26. {
  27. int i;
  28. for(i = 0; i < a->refcount; i ++) {
  29. ret->refs[ret->refcount] = a->refs[i];
  30. *ret->refs[ret->refcount++] = ret;
  31. }
  32. av_free(a->refs);
  33. av_free(a->formats);
  34. av_free(a);
  35. }
  36. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  37. {
  38. AVFilterFormats *ret;
  39. unsigned i, j, k = 0;
  40. if (a == b)
  41. return a;
  42. if (a == b)
  43. return a;
  44. ret = av_mallocz(sizeof(AVFilterFormats));
  45. /* merge list of formats */
  46. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  47. b->format_count));
  48. for(i = 0; i < a->format_count; i ++)
  49. for(j = 0; j < b->format_count; j ++)
  50. if(a->formats[i] == b->formats[j])
  51. ret->formats[k++] = a->formats[i];
  52. ret->format_count = k;
  53. /* check that there was at least one common format */
  54. if(!ret->format_count) {
  55. av_free(ret->formats);
  56. av_free(ret);
  57. return NULL;
  58. }
  59. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  60. merge_ref(ret, a);
  61. merge_ref(ret, b);
  62. return ret;
  63. }
  64. AVFilterFormats *avfilter_make_format_list(int len, ...)
  65. {
  66. AVFilterFormats *ret;
  67. int i;
  68. va_list vl;
  69. ret = av_mallocz(sizeof(AVFilterFormats));
  70. ret->formats = av_malloc(sizeof(*ret->formats) * len);
  71. ret->format_count = len;
  72. va_start(vl, len);
  73. for(i = 0; i < len; i ++)
  74. ret->formats[i] = va_arg(vl, int);
  75. va_end(vl);
  76. return ret;
  77. }
  78. AVFilterFormats *avfilter_all_colorspaces(void)
  79. {
  80. AVFilterFormats *ret;
  81. int i;
  82. ret = av_mallocz(sizeof(AVFilterFormats));
  83. ret->formats = av_malloc(sizeof(*ret->formats) * PIX_FMT_NB);
  84. ret->format_count = PIX_FMT_NB;
  85. for(i = 0; i < PIX_FMT_NB; i ++)
  86. ret->formats[i] = i;
  87. return ret;
  88. }
  89. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  90. {
  91. *ref = f;
  92. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  93. f->refs[f->refcount-1] = ref;
  94. }
  95. static int find_ref_index(AVFilterFormats **ref)
  96. {
  97. int i;
  98. for(i = 0; i < (*ref)->refcount; i ++)
  99. if((*ref)->refs[i] == ref)
  100. return i;
  101. return -1;
  102. }
  103. void avfilter_formats_unref(AVFilterFormats **ref)
  104. {
  105. int idx = find_ref_index(ref);
  106. if(idx >= 0)
  107. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  108. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  109. if(!--(*ref)->refcount) {
  110. av_free((*ref)->formats);
  111. av_free((*ref)->refs);
  112. av_free(*ref);
  113. }
  114. *ref = NULL;
  115. }
  116. void avfilter_formats_changeref(AVFilterFormats **oldref,
  117. AVFilterFormats **newref)
  118. {
  119. int idx = find_ref_index(oldref);
  120. if(idx >= 0) {
  121. (*oldref)->refs[idx] = newref;
  122. *newref = *oldref;
  123. *oldref = NULL;
  124. }
  125. }