libcdio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * libcdio CD grabbing
  23. */
  24. #include "config.h"
  25. #if HAVE_CDIO_PARANOIA_H
  26. #include <cdio/cdda.h>
  27. #include <cdio/paranoia.h>
  28. #elif HAVE_CDIO_PARANOIA_PARANOIA_H
  29. #include <cdio/paranoia/cdda.h>
  30. #include <cdio/paranoia/paranoia.h>
  31. #endif
  32. #include "libavutil/log.h"
  33. #include "libavutil/opt.h"
  34. #include "libavformat/avformat.h"
  35. #include "libavformat/internal.h"
  36. typedef struct CDIOContext {
  37. const AVClass *class;
  38. cdrom_drive_t *drive;
  39. cdrom_paranoia_t *paranoia;
  40. int32_t last_sector;
  41. /* private options */
  42. int speed;
  43. int paranoia_mode;
  44. } CDIOContext;
  45. static av_cold int read_header(AVFormatContext *ctx)
  46. {
  47. CDIOContext *s = ctx->priv_data;
  48. AVStream *st;
  49. int ret, i;
  50. char *err = NULL;
  51. if (!(st = avformat_new_stream(ctx, NULL)))
  52. return AVERROR(ENOMEM);
  53. s->drive = cdio_cddap_identify(ctx->url, CDDA_MESSAGE_LOGIT, &err);
  54. if (!s->drive) {
  55. av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->url);
  56. return AVERROR(EINVAL);
  57. }
  58. if (err) {
  59. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  60. free(err);
  61. }
  62. if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
  63. av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->url);
  64. return AVERROR(EINVAL);
  65. }
  66. cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
  67. if (s->speed)
  68. cdio_cddap_speed_set(s->drive, s->speed);
  69. s->paranoia = cdio_paranoia_init(s->drive);
  70. if (!s->paranoia) {
  71. av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
  72. return AVERROR(EINVAL);
  73. }
  74. cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
  75. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  76. if (s->drive->bigendianp)
  77. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  78. else
  79. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  80. st->codecpar->sample_rate = 44100;
  81. st->codecpar->channels = 2;
  82. if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
  83. s->drive->audio_first_sector != CDIO_INVALID_LSN)
  84. st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
  85. else if (s->drive->tracks)
  86. st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
  87. avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2 * st->codecpar->channels * st->codecpar->sample_rate);
  88. for (i = 0; i < s->drive->tracks; i++) {
  89. char title[16];
  90. snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
  91. avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
  92. s->drive->disc_toc[i+1].dwStartSector, title);
  93. }
  94. s->last_sector = cdio_cddap_disc_lastsector(s->drive);
  95. return 0;
  96. }
  97. static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
  98. {
  99. CDIOContext *s = ctx->priv_data;
  100. int ret;
  101. uint16_t *buf;
  102. char *err = NULL;
  103. buf = cdio_paranoia_read(s->paranoia, NULL);
  104. if (!buf)
  105. return AVERROR_EOF;
  106. if (err = cdio_cddap_errors(s->drive)) {
  107. av_log(ctx, AV_LOG_ERROR, "%s\n", err);
  108. free(err);
  109. err = NULL;
  110. }
  111. if (err = cdio_cddap_messages(s->drive)) {
  112. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  113. free(err);
  114. err = NULL;
  115. }
  116. if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
  117. return ret;
  118. memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
  119. return 0;
  120. }
  121. static av_cold int read_close(AVFormatContext *ctx)
  122. {
  123. CDIOContext *s = ctx->priv_data;
  124. cdio_paranoia_free(s->paranoia);
  125. cdio_cddap_close(s->drive);
  126. return 0;
  127. }
  128. static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
  129. int flags)
  130. {
  131. CDIOContext *s = ctx->priv_data;
  132. AVStream *st = ctx->streams[0];
  133. cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
  134. avpriv_update_cur_dts(ctx, st, timestamp);
  135. return 0;
  136. }
  137. #define OFFSET(x) offsetof(CDIOContext, x)
  138. #define DEC AV_OPT_FLAG_DECODING_PARAM
  139. static const AVOption options[] = {
  140. { "speed", "set drive reading speed", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
  141. { "paranoia_mode", "set error recovery mode", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = PARANOIA_MODE_DISABLE }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
  142. { "disable", "apply no fixups", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_DISABLE }, 0, 0, DEC, "paranoia_mode" },
  143. { "verify", "verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
  144. { "overlap", "perform overlapped reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
  145. { "neverskip", "do not skip failed reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
  146. { "full", "apply all recovery modes", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_FULL }, 0, 0, DEC, "paranoia_mode" },
  147. { NULL },
  148. };
  149. static const AVClass libcdio_class = {
  150. .class_name = "libcdio indev",
  151. .item_name = av_default_item_name,
  152. .option = options,
  153. .version = LIBAVUTIL_VERSION_INT,
  154. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  155. };
  156. const AVInputFormat ff_libcdio_demuxer = {
  157. .name = "libcdio",
  158. .read_header = read_header,
  159. .read_packet = read_packet,
  160. .read_close = read_close,
  161. .read_seek = read_seek,
  162. .priv_data_size = sizeof(CDIOContext),
  163. .flags = AVFMT_NOFILE,
  164. .priv_class = &libcdio_class,
  165. };