a64.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. avio_write(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. } else {
  94. av_log(avctx, AV_LOG_ERROR, "extradata not set\n");
  95. return AVERROR(EINVAL);
  96. }
  97. ch_chunksize=charset_size/lifetime;
  98. /* TODO: check if charset/size is % lifetime, but maybe check in codec */
  99. if(pkt->data) num_frames = lifetime;
  100. else num_frames = c->prev_frame_count;
  101. for(i = 0; i < num_frames; i++) {
  102. if(pkt->data) {
  103. /* if available, put newest charset chunk into buffer */
  104. avio_write(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);
  105. } else {
  106. /* a bit ugly, but is there an alternative to put many zeros? */
  107. for(j = 0; j < ch_chunksize; j++) avio_w8(s->pb, 0);
  108. }
  109. if(c->prev_pkt.data) {
  110. /* put frame (screen + colram) from last packet into buffer */
  111. avio_write(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);
  112. } else {
  113. /* a bit ugly, but is there an alternative to put many zeros? */
  114. for(j = 0; j < frame_size; j++) avio_w8(s->pb, 0);
  115. }
  116. }
  117. /* backup current packet for next turn */
  118. if(pkt->data) {
  119. /* no backup packet yet? create one! */
  120. if(!c->prev_pkt.data) av_new_packet(&c->prev_pkt, pkt->size);
  121. /* we have a packet and data is big enough, reuse it */
  122. if(c->prev_pkt.data && c->prev_pkt.size >= pkt->size) {
  123. memcpy(c->prev_pkt.data, pkt->data, pkt->size);
  124. c->prev_pkt.size = pkt->size;
  125. } else {
  126. av_log(avctx, AV_LOG_ERROR, "Too less memory for prev_pkt.\n");
  127. return AVERROR(ENOMEM);
  128. }
  129. }
  130. c->prev_frame_count = frame_count;
  131. break;
  132. }
  133. default:
  134. /* Write things as is. Nice for self-contained frames from non-multicolor modes or if played
  135. * directly from ram and not from a streaming device (rrnet/mmc) */
  136. if(pkt) avio_write(s->pb, pkt->data, pkt->size);
  137. break;
  138. }
  139. avio_flush(s->pb);
  140. return 0;
  141. }
  142. static int a64_write_trailer(struct AVFormatContext *s)
  143. {
  144. A64MuxerContext *c = s->priv_data;
  145. AVPacket pkt = {0};
  146. /* need to flush last packet? */
  147. if(c->interleaved) a64_write_packet(s, &pkt);
  148. /* discard backed up packet */
  149. if(c->prev_pkt.data) av_destruct_packet(&c->prev_pkt);
  150. return 0;
  151. }
  152. AVOutputFormat ff_a64_muxer = {
  153. .name = "a64",
  154. .long_name = NULL_IF_CONFIG_SMALL("a64 - video for Commodore 64"),
  155. .mime_type = NULL,
  156. .extensions = "a64, A64",
  157. .priv_data_size = sizeof (A64Context),
  158. .video_codec = CODEC_ID_A64_MULTI,
  159. a64_write_header,
  160. a64_write_packet,
  161. a64_write_trailer
  162. };