a64.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. }
  56. avio_write(s->pb, header, 2);
  57. c->prev_pkt.size = 0;
  58. c->prev_frame_count = 0;
  59. return 0;
  60. }
  61. static int a64_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  62. {
  63. AVCodecContext *avctx = s->streams[0]->codec;
  64. A64MuxerContext *c = s->priv_data;
  65. int i, j;
  66. int ch_chunksize;
  67. int lifetime;
  68. int frame_count;
  69. int charset_size;
  70. int frame_size;
  71. int num_frames;
  72. /* fetch values from extradata */
  73. switch (avctx->codec->id) {
  74. case CODEC_ID_A64_MULTI:
  75. case CODEC_ID_A64_MULTI5:
  76. if(c->interleaved) {
  77. /* Write interleaved, means we insert chunks of the future charset before each current frame.
  78. * Reason: if we load 1 charset + corresponding frames in one block on c64, we need to store
  79. * them first and then display frame by frame to keep in sync. Thus we would read and write
  80. * the data for colram from/to ram first and waste too much time. If we interleave and send the
  81. * charset beforehand, we assemble a new charset chunk by chunk, write current screen data to
  82. * screen-ram to be displayed and decode the colram directly to colram-location $d800 during
  83. * the overscan, while reading directly from source.
  84. * This is the only way so far, to achieve 25fps on c64 */
  85. if(avctx->extradata) {
  86. /* fetch values from extradata */
  87. lifetime = AV_RB32(avctx->extradata + 0);
  88. frame_count = AV_RB32(avctx->extradata + 4);
  89. charset_size = AV_RB32(avctx->extradata + 8);
  90. frame_size = AV_RB32(avctx->extradata + 12);
  91. /* TODO: sanity checks? */
  92. } else {
  93. av_log(avctx, AV_LOG_ERROR, "extradata not set\n");
  94. return AVERROR(EINVAL);
  95. }
  96. ch_chunksize=charset_size/lifetime;
  97. /* TODO: check if charset/size is % lifetime, but maybe check in codec */
  98. if(pkt->data) num_frames = lifetime;
  99. else num_frames = c->prev_frame_count;
  100. for(i = 0; i < num_frames; i++) {
  101. if(pkt->data) {
  102. /* if available, put newest charset chunk into buffer */
  103. avio_write(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);
  104. } else {
  105. /* a bit ugly, but is there an alternative to put many zeros? */
  106. for(j = 0; j < ch_chunksize; j++) avio_w8(s->pb, 0);
  107. }
  108. if(c->prev_pkt.data) {
  109. /* put frame (screen + colram) from last packet into buffer */
  110. avio_write(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);
  111. } else {
  112. /* a bit ugly, but is there an alternative to put many zeros? */
  113. for(j = 0; j < frame_size; j++) avio_w8(s->pb, 0);
  114. }
  115. }
  116. /* backup current packet for next turn */
  117. if(pkt->data) {
  118. /* no backup packet yet? create one! */
  119. if(!c->prev_pkt.data) av_new_packet(&c->prev_pkt, pkt->size);
  120. /* we have a packet and data is big enough, reuse it */
  121. if(c->prev_pkt.data && c->prev_pkt.size >= pkt->size) {
  122. memcpy(c->prev_pkt.data, pkt->data, pkt->size);
  123. c->prev_pkt.size = pkt->size;
  124. } else {
  125. av_log(avctx, AV_LOG_ERROR, "Too less memory for prev_pkt.\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. }
  129. c->prev_frame_count = frame_count;
  130. break;
  131. }
  132. default:
  133. /* Write things as is. Nice for self-contained frames from non-multicolor modes or if played
  134. * directly from ram and not from a streaming device (rrnet/mmc) */
  135. if(pkt) avio_write(s->pb, pkt->data, pkt->size);
  136. break;
  137. }
  138. avio_flush(s->pb);
  139. return 0;
  140. }
  141. static int a64_write_trailer(struct AVFormatContext *s)
  142. {
  143. A64MuxerContext *c = s->priv_data;
  144. AVPacket pkt = {0};
  145. /* need to flush last packet? */
  146. if(c->interleaved) a64_write_packet(s, &pkt);
  147. /* discard backed up packet */
  148. if(c->prev_pkt.data) av_destruct_packet(&c->prev_pkt);
  149. return 0;
  150. }
  151. AVOutputFormat ff_a64_muxer = {
  152. .name = "a64",
  153. .long_name = NULL_IF_CONFIG_SMALL("a64 - video for Commodore 64"),
  154. .mime_type = NULL,
  155. .extensions = "a64, A64",
  156. .priv_data_size = sizeof (A64Context),
  157. .video_codec = CODEC_ID_A64_MULTI,
  158. .write_header = a64_write_header,
  159. .write_packet = a64_write_packet,
  160. .write_trailer = a64_write_trailer
  161. };