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.

173 lines
4.5KB

  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. /**
  24. * Add all refs from a to ret and destroy a.
  25. */
  26. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  27. {
  28. int i;
  29. for(i = 0; i < a->refcount; i ++) {
  30. ret->refs[ret->refcount] = a->refs[i];
  31. *ret->refs[ret->refcount++] = ret;
  32. }
  33. av_free(a->refs);
  34. av_free(a->formats);
  35. av_free(a);
  36. }
  37. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  38. {
  39. AVFilterFormats *ret;
  40. unsigned i, j, k = 0;
  41. if (a == b)
  42. return a;
  43. ret = av_mallocz(sizeof(AVFilterFormats));
  44. /* merge list of formats */
  45. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  46. b->format_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. /* check that there was at least one common format */
  53. if(!ret->format_count) {
  54. av_free(ret->formats);
  55. av_free(ret);
  56. return NULL;
  57. }
  58. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  59. merge_ref(ret, a);
  60. merge_ref(ret, b);
  61. return ret;
  62. }
  63. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  64. {
  65. AVFilterFormats *formats;
  66. int count;
  67. for (count = 0; fmts[count] != -1; count++)
  68. ;
  69. formats = av_mallocz(sizeof(AVFilterFormats));
  70. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  71. formats->format_count = count;
  72. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  73. return formats;
  74. }
  75. int avfilter_add_format(AVFilterFormats **avff, int fmt)
  76. {
  77. int *fmts;
  78. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  79. return AVERROR(ENOMEM);
  80. fmts = av_realloc((*avff)->formats,
  81. sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
  82. if (!fmts)
  83. return AVERROR(ENOMEM);
  84. (*avff)->formats = fmts;
  85. (*avff)->formats[(*avff)->format_count++] = fmt;
  86. return 0;
  87. }
  88. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  89. {
  90. AVFilterFormats *ret = NULL;
  91. int fmt;
  92. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  93. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  94. for (fmt = 0; fmt < num_formats; fmt++)
  95. if ((type != AVMEDIA_TYPE_VIDEO) ||
  96. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  97. avfilter_add_format(&ret, fmt);
  98. return ret;
  99. }
  100. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  101. {
  102. *ref = f;
  103. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  104. f->refs[f->refcount-1] = ref;
  105. }
  106. static int find_ref_index(AVFilterFormats **ref)
  107. {
  108. int i;
  109. for(i = 0; i < (*ref)->refcount; i ++)
  110. if((*ref)->refs[i] == ref)
  111. return i;
  112. return -1;
  113. }
  114. void avfilter_formats_unref(AVFilterFormats **ref)
  115. {
  116. int idx;
  117. if (!*ref)
  118. return;
  119. idx = find_ref_index(ref);
  120. if(idx >= 0)
  121. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  122. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  123. if(!--(*ref)->refcount) {
  124. av_free((*ref)->formats);
  125. av_free((*ref)->refs);
  126. av_free(*ref);
  127. }
  128. *ref = NULL;
  129. }
  130. void avfilter_formats_changeref(AVFilterFormats **oldref,
  131. AVFilterFormats **newref)
  132. {
  133. int idx = find_ref_index(oldref);
  134. if(idx >= 0) {
  135. (*oldref)->refs[idx] = newref;
  136. *newref = *oldref;
  137. *oldref = NULL;
  138. }
  139. }