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.

165 lines
4.0KB

  1. /*
  2. * Raw DV format
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "dvcore.h"
  21. typedef struct DVDemuxContext {
  22. int is_audio;
  23. uint8_t buf[144000];
  24. int size;
  25. } DVDemuxContext;
  26. /* raw input */
  27. static int dv_read_header(AVFormatContext *s,
  28. AVFormatParameters *ap)
  29. {
  30. AVStream *vst, *ast;
  31. DVDemuxContext *c = s->priv_data;
  32. vst = av_new_stream(s, 0);
  33. if (!vst)
  34. return AVERROR_NOMEM;
  35. vst->codec.codec_type = CODEC_TYPE_VIDEO;
  36. vst->codec.codec_id = CODEC_ID_DVVIDEO;
  37. vst->codec.bit_rate = 25000000;
  38. ast = av_new_stream(s, 1);
  39. if (!ast)
  40. return AVERROR_NOMEM;
  41. ast->codec.codec_type = CODEC_TYPE_AUDIO;
  42. ast->codec.codec_id = CODEC_ID_DVAUDIO;
  43. c->is_audio = 0;
  44. return 0;
  45. }
  46. static void __destruct_pkt(struct AVPacket *pkt)
  47. {
  48. pkt->data = NULL; pkt->size = 0;
  49. return;
  50. }
  51. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  52. {
  53. int ret;
  54. DVDemuxContext *c = s->priv_data;
  55. if (!c->is_audio) {
  56. ret = get_buffer(&s->pb, c->buf, 4);
  57. if (ret <= 0)
  58. return -EIO;
  59. c->size = dv_frame_profile(&c->buf[0])->frame_size;
  60. ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
  61. if (ret <= 0)
  62. return -EIO;
  63. }
  64. av_init_packet(pkt);
  65. pkt->destruct = __destruct_pkt;
  66. pkt->data = c->buf;
  67. pkt->size = c->size;
  68. pkt->stream_index = c->is_audio;
  69. pkt->flags |= PKT_FLAG_KEY;
  70. c->is_audio = !c->is_audio;
  71. return c->size;
  72. }
  73. static int dv_read_close(AVFormatContext *s)
  74. {
  75. return 0;
  76. }
  77. int dv_write_header(struct AVFormatContext *s)
  78. {
  79. DVMuxContext *c = s->priv_data;
  80. if (s->nb_streams != 2 || dv_core_init(c, s->streams) != 0) {
  81. fprintf(stderr, "Can't initialize DV format!\n"
  82. "Make sure that you supply exactly two streams:\n"
  83. " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n");
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. int dv_write_packet(struct AVFormatContext *s,
  89. int stream_index,
  90. const uint8_t *buf, int size, int64_t pts)
  91. {
  92. DVMuxContext *c = s->priv_data;
  93. if (stream_index == c->vst)
  94. dv_assemble_frame(c, buf, NULL, 0);
  95. else
  96. dv_assemble_frame(c, NULL, buf, size);
  97. if (c->has_audio && c->has_video) {
  98. put_buffer(&s->pb, &c->frame_buf[0], c->sys->frame_size);
  99. put_flush_packet(&s->pb);
  100. }
  101. return 0;
  102. }
  103. /*
  104. * We might end up with some extra A/V data without matching counterpart.
  105. * E.g. video data without enough audio to write the complete frame.
  106. * Currently we simply drop the last frame. I don't know whether this
  107. * is the best strategy of all
  108. */
  109. int dv_write_trailer(struct AVFormatContext *s)
  110. {
  111. dv_core_delete((DVMuxContext *)s->priv_data);
  112. return 0;
  113. }
  114. static AVInputFormat dv_iformat = {
  115. "dv",
  116. "DV video format",
  117. sizeof(DVDemuxContext),
  118. NULL,
  119. dv_read_header,
  120. dv_read_packet,
  121. dv_read_close,
  122. .extensions = "dv",
  123. };
  124. AVOutputFormat dv_oformat = {
  125. "dv",
  126. "DV video format",
  127. NULL,
  128. "dv",
  129. sizeof(DVMuxContext),
  130. CODEC_ID_PCM_S16LE,
  131. CODEC_ID_DVVIDEO,
  132. dv_write_header,
  133. dv_write_packet,
  134. dv_write_trailer,
  135. };
  136. int dv_init(void)
  137. {
  138. av_register_input_format(&dv_iformat);
  139. av_register_output_format(&dv_oformat);
  140. return 0;
  141. }