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.

187 lines
4.7KB

  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, m_count;
  42. if (a == b)
  43. return a;
  44. ret = av_mallocz(sizeof(*ret));
  45. /* merge list of formats */
  46. m_count = FFMIN(a->format_count, b->format_count);
  47. if (m_count) {
  48. ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
  49. for(i = 0; i < a->format_count; i ++)
  50. for(j = 0; j < b->format_count; j ++)
  51. if(a->formats[i] == b->formats[j])
  52. ret->formats[k++] = a->formats[i];
  53. ret->format_count = k;
  54. }
  55. /* check that there was at least one common format */
  56. if(!ret->format_count) {
  57. av_free(ret->formats);
  58. av_free(ret);
  59. return NULL;
  60. }
  61. ret->refs = av_malloc(sizeof(*ret->refs) * (a->refcount + b->refcount));
  62. merge_ref(ret, a);
  63. merge_ref(ret, b);
  64. return ret;
  65. }
  66. int ff_fmt_is_in(int fmt, const int *fmts)
  67. {
  68. const int *p;
  69. for (p = fmts; *p != PIX_FMT_NONE; p++) {
  70. if (fmt == *p)
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  76. {
  77. AVFilterFormats *formats;
  78. int count;
  79. for (count = 0; fmts[count] != -1; count++)
  80. ;
  81. formats = av_mallocz(sizeof(*formats));
  82. if (count)
  83. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  84. formats->format_count = count;
  85. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  86. return formats;
  87. }
  88. int avfilter_add_format(AVFilterFormats **avff, int fmt)
  89. {
  90. int *fmts;
  91. if (!(*avff) && !(*avff = av_mallocz(sizeof(**avff))))
  92. return AVERROR(ENOMEM);
  93. fmts = av_realloc((*avff)->formats,
  94. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  95. if (!fmts)
  96. return AVERROR(ENOMEM);
  97. (*avff)->formats = fmts;
  98. (*avff)->formats[(*avff)->format_count++] = fmt;
  99. return 0;
  100. }
  101. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  102. {
  103. AVFilterFormats *ret = NULL;
  104. int fmt;
  105. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  106. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  107. for (fmt = 0; fmt < num_formats; fmt++)
  108. if ((type != AVMEDIA_TYPE_VIDEO) ||
  109. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  110. avfilter_add_format(&ret, fmt);
  111. return ret;
  112. }
  113. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  114. {
  115. *ref = f;
  116. f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount);
  117. f->refs[f->refcount-1] = ref;
  118. }
  119. static int find_ref_index(AVFilterFormats **ref)
  120. {
  121. int i;
  122. for(i = 0; i < (*ref)->refcount; i ++)
  123. if((*ref)->refs[i] == ref)
  124. return i;
  125. return -1;
  126. }
  127. void avfilter_formats_unref(AVFilterFormats **ref)
  128. {
  129. int idx;
  130. if (!*ref)
  131. return;
  132. idx = find_ref_index(ref);
  133. if(idx >= 0)
  134. memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,
  135. sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1));
  136. if(!--(*ref)->refcount) {
  137. av_free((*ref)->formats);
  138. av_free((*ref)->refs);
  139. av_free(*ref);
  140. }
  141. *ref = NULL;
  142. }
  143. void avfilter_formats_changeref(AVFilterFormats **oldref,
  144. AVFilterFormats **newref)
  145. {
  146. int idx = find_ref_index(oldref);
  147. if(idx >= 0) {
  148. (*oldref)->refs[idx] = newref;
  149. *newref = *oldref;
  150. *oldref = NULL;
  151. }
  152. }