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.

175 lines
4.5KB

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