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.8KB

  1. /*
  2. * Format register and lookup
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  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 "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/avstring.h"
  24. /**
  25. * @file
  26. * Format register and lookup
  27. */
  28. /** head of registered input format linked list */
  29. static AVInputFormat *first_iformat = NULL;
  30. /** head of registered output format linked list */
  31. static AVOutputFormat *first_oformat = NULL;
  32. AVInputFormat *av_iformat_next(AVInputFormat *f)
  33. {
  34. if (f)
  35. return f->next;
  36. else
  37. return first_iformat;
  38. }
  39. AVOutputFormat *av_oformat_next(AVOutputFormat *f)
  40. {
  41. if (f)
  42. return f->next;
  43. else
  44. return first_oformat;
  45. }
  46. void av_register_input_format(AVInputFormat *format)
  47. {
  48. AVInputFormat **p = &first_iformat;
  49. while (*p != NULL)
  50. p = &(*p)->next;
  51. *p = format;
  52. format->next = NULL;
  53. }
  54. void av_register_output_format(AVOutputFormat *format)
  55. {
  56. AVOutputFormat **p = &first_oformat;
  57. while (*p != NULL)
  58. p = &(*p)->next;
  59. *p = format;
  60. format->next = NULL;
  61. }
  62. int av_match_ext(const char *filename, const char *extensions)
  63. {
  64. const char *ext, *p;
  65. char ext1[32], *q;
  66. if (!filename)
  67. return 0;
  68. ext = strrchr(filename, '.');
  69. if (ext) {
  70. ext++;
  71. p = extensions;
  72. for (;;) {
  73. q = ext1;
  74. while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
  75. *q++ = *p++;
  76. *q = '\0';
  77. if (!av_strcasecmp(ext1, ext))
  78. return 1;
  79. if (*p == '\0')
  80. break;
  81. p++;
  82. }
  83. }
  84. return 0;
  85. }
  86. static int match_format(const char *name, const char *names)
  87. {
  88. const char *p;
  89. int len, namelen;
  90. if (!name || !names)
  91. return 0;
  92. namelen = strlen(name);
  93. while ((p = strchr(names, ','))) {
  94. len = FFMAX(p - names, namelen);
  95. if (!av_strncasecmp(name, names, len))
  96. return 1;
  97. names = p + 1;
  98. }
  99. return !av_strcasecmp(name, names);
  100. }
  101. AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  102. const char *mime_type)
  103. {
  104. AVOutputFormat *fmt = NULL, *fmt_found;
  105. int score_max, score;
  106. /* specific test for image sequences */
  107. #if CONFIG_IMAGE2_MUXER
  108. if (!short_name && filename &&
  109. av_filename_number_test(filename) &&
  110. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  111. return av_guess_format("image2", NULL, NULL);
  112. }
  113. #endif
  114. /* Find the proper file type. */
  115. fmt_found = NULL;
  116. score_max = 0;
  117. while ((fmt = av_oformat_next(fmt))) {
  118. score = 0;
  119. if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
  120. score += 100;
  121. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  122. score += 10;
  123. if (filename && fmt->extensions &&
  124. av_match_ext(filename, fmt->extensions)) {
  125. score += 5;
  126. }
  127. if (score > score_max) {
  128. score_max = score;
  129. fmt_found = fmt;
  130. }
  131. }
  132. return fmt_found;
  133. }
  134. enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  135. const char *filename, const char *mime_type,
  136. enum AVMediaType type)
  137. {
  138. if (type == AVMEDIA_TYPE_VIDEO) {
  139. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  140. #if CONFIG_IMAGE2_MUXER
  141. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  142. codec_id = ff_guess_image2_codec(filename);
  143. }
  144. #endif
  145. if (codec_id == AV_CODEC_ID_NONE)
  146. codec_id = fmt->video_codec;
  147. return codec_id;
  148. } else if (type == AVMEDIA_TYPE_AUDIO)
  149. return fmt->audio_codec;
  150. else if (type == AVMEDIA_TYPE_SUBTITLE)
  151. return fmt->subtitle_codec;
  152. else
  153. return AV_CODEC_ID_NONE;
  154. }
  155. AVInputFormat *av_find_input_format(const char *short_name)
  156. {
  157. AVInputFormat *fmt = NULL;
  158. while ((fmt = av_iformat_next(fmt)))
  159. if (match_format(short_name, fmt->name))
  160. return fmt;
  161. return NULL;
  162. }