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.

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