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.

147 lines
3.7KB

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