You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.8KB

  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. 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. AVFilterFormats *avfilter_make_format_list(int len, ...)
  63. {
  64. AVFilterFormats *ret;
  65. int i;
  66. va_list vl;
  67. ret = av_mallocz(sizeof(AVFilterFormats));
  68. ret->formats = av_malloc(sizeof(*ret->formats) * len);
  69. ret->format_count = len;
  70. va_start(vl, len);
  71. for(i = 0; i < len; i ++)
  72. ret->formats[i] = va_arg(vl, int);
  73. va_end(vl);
  74. return ret;
  75. }
  76. AVFilterFormats *avfilter_all_colorspaces(void)
  77. {
  78. AVFilterFormats *ret;
  79. int i;
  80. ret = av_mallocz(sizeof(AVFilterFormats));
  81. ret->formats = av_malloc(sizeof(*ret->formats) * PIX_FMT_NB);
  82. ret->format_count = PIX_FMT_NB;
  83. for(i = 0; i < PIX_FMT_NB; i ++)
  84. ret->formats[i] = i;
  85. return ret;
  86. }
  87. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  88. {
  89. *ref = f;
  90. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  91. f->refs[f->refcount-1] = ref;
  92. }
  93. static int find_ref_index(AVFilterFormats **ref)
  94. {
  95. int i;
  96. for(i = 0; i < (*ref)->refcount; i ++)
  97. if((*ref)->refs[i] == ref)
  98. return i;
  99. return -1;
  100. }
  101. void avfilter_formats_unref(AVFilterFormats **ref)
  102. {
  103. int idx = find_ref_index(ref);
  104. if(idx >= 0)
  105. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  106. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  107. if(!--(*ref)->refcount) {
  108. av_free((*ref)->formats);
  109. av_free((*ref)->refs);
  110. av_free(*ref);
  111. }
  112. *ref = NULL;
  113. }
  114. void avfilter_formats_changeref(AVFilterFormats **oldref,
  115. AVFilterFormats **newref)
  116. {
  117. int idx = find_ref_index(oldref);
  118. if(idx >= 0) {
  119. (*oldref)->refs[idx] = newref;
  120. *newref = *oldref;
  121. *oldref = NULL;
  122. }
  123. }