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
5.8KB

  1. /*
  2. * a64 muxer
  3. * Copyright (c) 2009 Tobias Bindhammer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/avcodec.h"
  22. #include "libavcodec/a64enc.h"
  23. #include "libavcodec/bytestream.h"
  24. #include "avformat.h"
  25. typedef struct A64MuxerContext {
  26. int interleaved;
  27. AVPacket prev_pkt;
  28. int prev_frame_count;
  29. } A64MuxerContext;
  30. static int a64_write_header(struct AVFormatContext *s)
  31. {
  32. AVCodecContext *avctx = s->streams[0]->codec;
  33. A64MuxerContext *c = s->priv_data;
  34. uint8_t header[5] = {
  35. 0x00, //load
  36. 0x40, //address
  37. 0x00, //mode
  38. 0x00, //charset_lifetime (multi only)
  39. 0x00 //fps in 50/fps;
  40. };
  41. c->interleaved = 0;
  42. switch (avctx->codec->id) {
  43. case CODEC_ID_A64_MULTI:
  44. header[2] = 0x00;
  45. header[3] = AV_RB32(avctx->extradata+0);
  46. header[4] = 2;
  47. break;
  48. case CODEC_ID_A64_MULTI5:
  49. header[2] = 0x01;
  50. header[3] = AV_RB32(avctx->extradata+0);
  51. header[4] = 3;
  52. break;
  53. default:
  54. return AVERROR(EINVAL);
  55. break;
  56. }
  57. put_buffer(s->pb, header, 2);
  58. c->prev_pkt.size = 0;
  59. c->prev_frame_count = 0;
  60. return 0;
  61. }
  62. static int a64_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  63. {
  64. AVCodecContext *avctx = s->streams[0]->codec;
  65. A64MuxerContext *c = s->priv_data;
  66. int i, j;
  67. int ch_chunksize;
  68. int lifetime;
  69. int frame_count;
  70. int charset_size;
  71. int frame_size;
  72. int num_frames;
  73. /* fetch values from extradata */
  74. switch (avctx->codec->id) {
  75. case CODEC_ID_A64_MULTI:
  76. case CODEC_ID_A64_MULTI5:
  77. if(c->interleaved) {
  78. /* Write interleaved, means we insert chunks of the future charset before each current frame.
  79. * Reason: if we load 1 charset + corresponding frames in one block on c64, we need to store
  80. * them first and then display frame by frame to keep in sync. Thus we would read and write
  81. * the data for colram from/to ram first and waste too much time. If we interleave and send the
  82. * charset beforehand, we assemble a new charset chunk by chunk, write current screen data to
  83. * screen-ram to be displayed and decode the colram directly to colram-location $d800 during
  84. * the overscan, while reading directly from source
  85. * This is the only way so far, to achieve 25fps on c64 */
  86. if(avctx->extradata) {
  87. /* fetch values from extradata */
  88. lifetime = AV_RB32(avctx->extradata + 0);
  89. frame_count = AV_RB32(avctx->extradata + 4);
  90. charset_size = AV_RB32(avctx->extradata + 8);
  91. frame_size = AV_RB32(avctx->extradata + 12);
  92. /* TODO: sanity checks? */
  93. }
  94. else {
  95. av_log(avctx, AV_LOG_ERROR, "extradata not set\n");
  96. return AVERROR(EINVAL);
  97. }
  98. ch_chunksize=charset_size/lifetime;
  99. /* TODO: check if charset/size is % lifetime, but maybe check in codec */
  100. if(pkt->data) num_frames = lifetime;
  101. else num_frames = c->prev_frame_count;
  102. for(i = 0; i < num_frames; i++) {
  103. if(pkt->data) {
  104. /* if available, put newest charset chunk into buffer */
  105. put_buffer(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);
  106. }
  107. else {
  108. /* a bit ugly, but is there an alternative to put many zeros? */
  109. for(j = 0; j < ch_chunksize; j++) put_byte(s->pb, 0);
  110. }
  111. if(c->prev_pkt.data) {
  112. /* put frame (screen + colram) from last packet into buffer */
  113. put_buffer(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);
  114. }
  115. else {
  116. /* a bit ugly, but is there an alternative to put many zeros? */
  117. for(j = 0; j < frame_size; j++) put_byte(s->pb, 0);
  118. }
  119. }
  120. /* backup current packet for next turn */
  121. if(pkt->data) {
  122. av_new_packet(&c->prev_pkt, pkt->size);
  123. memcpy(c->prev_pkt.data, pkt->data, pkt->size);
  124. }
  125. c->prev_frame_count = frame_count;
  126. break;
  127. }
  128. default:
  129. /* Write things as is. Nice for self-contained frames from non-multicolor modes or if played
  130. * directly from ram and not from a streaming device (rrnet/mmc) */
  131. if(pkt) put_buffer(s->pb, pkt->data, pkt->size);
  132. break;
  133. }
  134. put_flush_packet(s->pb);
  135. return 0;
  136. }
  137. static int a64_write_trailer(struct AVFormatContext *s)
  138. {
  139. A64MuxerContext *c = s->priv_data;
  140. AVPacket pkt;
  141. /* need to flush last packet? */
  142. if(c->interleaved) a64_write_packet(s, &pkt);
  143. return 0;
  144. }
  145. AVOutputFormat a64_muxer = {
  146. .name = "a64",
  147. .long_name = NULL_IF_CONFIG_SMALL("a64 - video for Commodore 64"),
  148. .mime_type = NULL,
  149. .extensions = "a64, A64",
  150. .priv_data_size = sizeof (A64Context),
  151. .video_codec = CODEC_ID_A64_MULTI,
  152. a64_write_header,
  153. a64_write_packet,
  154. a64_write_trailer
  155. };