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.

187 lines
6.0KB

  1. /*
  2. * Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 <cdio/cdda.h>
  25. #include <cdio/paranoia.h>
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavformat/avformat.h"
  30. #include "libavformat/internal.h"
  31. /* cdio returns some malloced strings that need to be free()d */
  32. #undef free
  33. typedef struct CDIOContext {
  34. cdrom_drive_t *drive;
  35. cdrom_paranoia_t *paranoia;
  36. int32_t last_sector;
  37. /* private options */
  38. int speed;
  39. int paranoia_mode;
  40. } CDIOContext;
  41. static av_cold int read_header(AVFormatContext *ctx, AVFormatParameters *ap)
  42. {
  43. CDIOContext *s = ctx->priv_data;
  44. AVStream *st;
  45. int ret, i;
  46. char *err = NULL;
  47. if (!(st = avformat_new_stream(ctx, NULL)))
  48. return AVERROR(ENOMEM);
  49. s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
  50. if (!s->drive) {
  51. av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
  52. return AVERROR(EINVAL);
  53. }
  54. if (err) {
  55. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  56. free(err);
  57. }
  58. if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
  59. av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
  60. return AVERROR(EINVAL);
  61. }
  62. cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
  63. if (s->speed)
  64. cdio_cddap_speed_set(s->drive, s->speed);
  65. s->paranoia = cdio_paranoia_init(s->drive);
  66. if (!s->paranoia) {
  67. av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
  68. return AVERROR(EINVAL);
  69. }
  70. cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
  71. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  72. if (s->drive->bigendianp)
  73. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  74. else
  75. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  76. st->codec->sample_rate = 44100;
  77. st->codec->channels = 2;
  78. if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
  79. s->drive->audio_first_sector != CDIO_INVALID_LSN)
  80. st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
  81. else if (s->drive->tracks)
  82. st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
  83. av_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
  84. for (i = 0; i < s->drive->tracks; i++) {
  85. char title[16];
  86. snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
  87. avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
  88. s->drive->disc_toc[i+1].dwStartSector, title);
  89. }
  90. s->last_sector = cdio_cddap_disc_lastsector(s->drive);
  91. return 0;
  92. }
  93. static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
  94. {
  95. CDIOContext *s = ctx->priv_data;
  96. int ret;
  97. uint16_t *buf;
  98. char *err = NULL;
  99. if (ctx->streams[0]->cur_dts > s->last_sector)
  100. return AVERROR_EOF;
  101. buf = cdio_paranoia_read(s->paranoia, NULL);
  102. if (!buf)
  103. return AVERROR_EOF;
  104. if (err = cdio_cddap_errors(s->drive)) {
  105. av_log(ctx, AV_LOG_ERROR, "%s\n", err);
  106. free(err);
  107. err = NULL;
  108. }
  109. if (err = cdio_cddap_messages(s->drive)) {
  110. av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
  111. free(err);
  112. err = NULL;
  113. }
  114. if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
  115. return ret;
  116. memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
  117. return 0;
  118. }
  119. static av_cold int read_close(AVFormatContext *ctx)
  120. {
  121. CDIOContext *s = ctx->priv_data;
  122. cdio_paranoia_free(s->paranoia);
  123. cdio_cddap_close(s->drive);
  124. return 0;
  125. }
  126. static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
  127. int flags)
  128. {
  129. CDIOContext *s = ctx->priv_data;
  130. AVStream *st = ctx->streams[0];
  131. cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
  132. st->cur_dts = timestamp;
  133. return 0;
  134. }
  135. #define OFFSET(x) offsetof(CDIOContext, x)
  136. #define DEC AV_OPT_FLAG_DECODING_PARAM
  137. static const AVOption options[] = {
  138. { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, DEC },
  139. { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
  140. { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
  141. { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
  142. { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
  143. { NULL },
  144. };
  145. static const AVClass libcdio_class = {
  146. .class_name = "libcdio indev",
  147. .item_name = av_default_item_name,
  148. .option = options,
  149. .version = LIBAVUTIL_VERSION_INT,
  150. };
  151. AVInputFormat ff_libcdio_demuxer = {
  152. .name = "libcdio",
  153. .read_header = read_header,
  154. .read_packet = read_packet,
  155. .read_close = read_close,
  156. .read_seek = read_seek,
  157. .priv_data_size = sizeof(CDIOContext),
  158. .flags = AVFMT_NOFILE,
  159. .priv_class = &libcdio_class,
  160. };