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.

142 lines
3.2KB

  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. #define NTSC_FRAME_SIZE 120000
  21. #define PAL_FRAME_SIZE 144000
  22. typedef struct DVDemuxContext {
  23. int is_audio;
  24. uint8_t buf[PAL_FRAME_SIZE];
  25. int size;
  26. } DVDemuxContext;
  27. /* raw input */
  28. static int dv_read_header(AVFormatContext *s,
  29. AVFormatParameters *ap)
  30. {
  31. AVStream *vst, *ast;
  32. DVDemuxContext *c = s->priv_data;
  33. vst = av_new_stream(s, 0);
  34. if (!vst)
  35. return AVERROR_NOMEM;
  36. vst->codec.codec_type = CODEC_TYPE_VIDEO;
  37. vst->codec.codec_id = CODEC_ID_DVVIDEO;
  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. ast->codec.channels = 2;
  44. c->is_audio = 0;
  45. return 0;
  46. }
  47. /* XXX: build fake audio stream when DV audio decoder will be finished */
  48. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  49. {
  50. int ret, dsf;
  51. DVDemuxContext *c = s->priv_data;
  52. if (!c->is_audio) {
  53. ret = get_buffer(&s->pb, c->buf, 4);
  54. if (ret <= 0)
  55. return -EIO;
  56. dsf = c->buf[3] & 0x80;
  57. if (!dsf)
  58. c->size = NTSC_FRAME_SIZE;
  59. else
  60. c->size = PAL_FRAME_SIZE;
  61. ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
  62. if (ret <= 0)
  63. return -EIO;
  64. }
  65. if (av_new_packet(pkt, c->size) < 0)
  66. return -EIO;
  67. pkt->stream_index = c->is_audio;
  68. c->is_audio = !c->is_audio;
  69. memcpy(pkt->data, c->buf, c->size);
  70. return ret;
  71. }
  72. static int dv_read_close(AVFormatContext *s)
  73. {
  74. return 0;
  75. }
  76. static AVInputFormat dv_iformat = {
  77. "dv",
  78. "DV video format",
  79. sizeof(DVDemuxContext),
  80. NULL,
  81. dv_read_header,
  82. dv_read_packet,
  83. dv_read_close,
  84. .extensions = "dv",
  85. };
  86. #if 0
  87. int dv_write_header(struct AVFormatContext *s)
  88. {
  89. return 0;
  90. }
  91. int dv_write_packet(struct AVFormatContext *s,
  92. int stream_index,
  93. unsigned char *buf, int size, int force_pts)
  94. {
  95. put_buffer(&s->pb, buf, size);
  96. put_flush_packet(&s->pb);
  97. return 0;
  98. }
  99. int dv_write_trailer(struct AVFormatContext *s)
  100. {
  101. return 0;
  102. }
  103. AVOutputFormat dv_oformat = {
  104. "dv",
  105. "DV video format",
  106. NULL,
  107. "dv",
  108. 0,
  109. CODEC_ID_DVVIDEO,
  110. CODEC_ID_DVAUDIO,
  111. dv_write_header,
  112. dv_write_packet,
  113. dv_write_trailer,
  114. };
  115. #endif
  116. int dv_init(void)
  117. {
  118. av_register_input_format(&dv_iformat);
  119. // av_register_output_format(&dv_oformat);
  120. return 0;
  121. }