1
0

nsvencode.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef _NSVENCODE_H_
  2. #define _NSVENCODE_H_
  3. #include "../jnetlib/jnetlib.h"
  4. #include "nsvlib.h"
  5. #include "enc_if.h"
  6. #include <stdio.h>
  7. class nsv_Encoder
  8. {
  9. public:
  10. nsv_Encoder();
  11. ~nsv_Encoder();
  12. static void freeCoders(); // call this before quitting if you used any nsvencoders
  13. static void loadCoders();
  14. static HWND configAudio(unsigned int audfmt, HWND hwndParent, char *configfile);
  15. static HWND configVideo(unsigned int vidfmt, HWND hwndParent, char *configfile);
  16. static int enumAudio(int *state, char *name, unsigned int *fmt);
  17. static int enumVideo(int *state, char *name, unsigned int *fmt);
  18. char *getError() { return m_err; } // NULL on no error
  19. // compressor configuration (do not call these after calling openOutput())
  20. void setVideo(unsigned int vfmt, unsigned int w, unsigned int h, double frt,
  21. unsigned int srcfmt=NSV_MAKETYPE('R','G','B','A'),
  22. char *configfile=NULL);
  23. void setAudio(unsigned int afmt, int srate, int nch, int bps,
  24. unsigned int srcfmt=NSV_MAKETYPE('P','C','M',' '),
  25. char *configfile=NULL);
  26. // stream configuration (do not call these after calling openOutput())
  27. void setMaxAudioSendAhead(int offs) { m_audio_ahead_of_video=offs; }
  28. void setSyncFrameInterval(int minimum=0, int maximum=120) { m_max_syncframe_dist=maximum; m_min_syncframe_dist=minimum; }
  29. // setting one of these will enable file header writing on files
  30. // do NOT call these after calling openOutput(),
  31. int addHdrMetaData(char *name, char *data, int data_len); // returns nonzero on error
  32. int setHdrMetaData(char *data, int data_len); // nonzero on error
  33. void setHdrTOCSize(int tocsize);
  34. void forceHdrWrite() { m_usefilehdr=1; } // use if you dont want meta or toc, but want a basic header
  35. // output (call only once, do not close and reopen (yet))
  36. void openOutput(char *url); // call getError to check for error
  37. void closeOutput();
  38. // encode (call only after openoutput())
  39. void addAudioData(void *data, int data_len);
  40. int needAudioData(); // returns 1 if more audio data would be nice for sync, 2 if there is no audio data in buffer
  41. // for audio only, use compressor type NONE and call this with NULL
  42. void addVideoFrame(void *frame);
  43. // calling this will put the aux data in the same frame as the NEXT addVideoFrame()
  44. void addAuxData(unsigned int fmt, void *data, int data_len);
  45. // setting audio eof will let the audio compressor know that it is the end of stream
  46. // so that it can flush (via passing an empty frame to it)
  47. void set_audioEof(int eof) { m_audio_eof=!!eof; }
  48. int get_audioEof() { return m_audio_eof; }
  49. // stats
  50. unsigned int getCurrentBitrate()
  51. {
  52. if (m_video_position_frames)
  53. return (unsigned int) ((double) m_bitstreambytesout / ((double) m_video_position_frames / m_framerate) * 8.0);
  54. return 0;
  55. }
  56. private:
  57. char *m_err;
  58. int m_usefilehdr;
  59. unsigned int m_tocsize;
  60. int m_audio_channels;
  61. int m_audio_samplerate;
  62. int m_audio_bits;
  63. unsigned int m_audio_srcfmt;
  64. unsigned int m_audio_coder;
  65. char *m_audio_configfile;
  66. double m_framerate;
  67. unsigned int m_w, m_h;
  68. unsigned int m_video_coder;
  69. unsigned int m_video_srcfmt;
  70. char *m_video_configfile;
  71. int m_audio_ahead_of_video;
  72. int m_max_syncframe_dist,m_min_syncframe_dist;
  73. nsv_fileHeader m_filehdr;
  74. FILE *m_outfile;
  75. JNL_Connection *m_outcon;
  76. int m_is_uvox; // for m_outco
  77. char *m_outcon_host,*m_outcon_headers;
  78. int m_outcon_port;
  79. unsigned int m_outcon_last_connect_time;
  80. nsv_Packeter m_packeter;
  81. nsv_InBS m_outconbs;
  82. __int64 m_bitstreambytesout;
  83. AudioCoder *m_audcoder;
  84. VideoCoder *m_vidcoder;
  85. __int64 m_audio_position_bytes;
  86. __int64 m_video_position_frames;
  87. char m_vidobuf[NSV_MAX_VIDEO_LEN];
  88. char m_audobuf[NSV_MAX_AUDIO_LEN];
  89. nsv_InBS m_audibuf;
  90. unsigned int m_total_frames;
  91. nsv_GrowBuf m_tocEntries;
  92. int m_frames_since_last_sync_frame;
  93. int m_audio_eof;
  94. void send_uvoxmessage(int msgclass, int msgtype, void *msg, int msglen);
  95. static VideoCoder *init_video(int w, int h, double frt, unsigned int pixt, unsigned int *outt, char *configfile);
  96. static AudioCoder *init_audio(int nch, int srate, int bps, unsigned int srct, unsigned int *outt, char *configfile);
  97. static int Coders_loaded;
  98. static HINSTANCE g_Coders[256];
  99. static int g_numCoders;
  100. static char g_dll_dir[MAX_PATH];
  101. static BOOL CALLBACK PcmProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam);
  102. };
  103. class nsv_EncoderConfigFile
  104. {
  105. public:
  106. nsv_EncoderConfigFile(char *filename=NULL)
  107. {
  108. m_filename=filename?_strdup(filename):0;
  109. m_audfmt=m_vidfmt=0;
  110. m_minsync=0;
  111. m_maxsync=120;
  112. m_audiosendahead=0;
  113. m_hdrwrite=0;
  114. m_hdrtoc=1024;
  115. m_metadata=0;
  116. m_crop_l=0; // ##
  117. m_crop_r=0; // ##
  118. m_crop_t=0; // ##
  119. m_crop_b=0; // ##
  120. ReadIn();
  121. }
  122. ~nsv_EncoderConfigFile(); //ak 7-23-03 moved so breakpoint can be set
  123. unsigned int getVidFmt() { return m_vidfmt; }
  124. unsigned int getAudFmt() { return m_audfmt; }
  125. void setVidFmt(unsigned int fmt) { m_vidfmt=fmt; }
  126. void setAudFmt(unsigned int fmt) { m_audfmt=fmt; }
  127. char *getFilename() { return m_filename; }
  128. void setFilename(char *filename)
  129. {
  130. FlushOut();
  131. free(m_filename);
  132. if (filename)
  133. {
  134. m_filename=_strdup(filename);
  135. ReadIn();
  136. }
  137. else m_filename=0;
  138. }
  139. int getMinSyncFrameInt() { return m_minsync; }
  140. void setMinSyncFrameInt(int minint) { m_minsync=minint; }
  141. int getMaxSyncFrameInt() { return m_maxsync; }
  142. void setMaxSyncFrameInt(int maxint) { m_maxsync=maxint; }
  143. int getAudioSendAhead() { return m_audiosendahead; }
  144. void setAudioSendAhead(int sa) { m_audiosendahead=sa; }
  145. char *hdrGetMetadata() { return m_metadata; }
  146. int hdrGetDoWrite() { return m_hdrwrite; }
  147. int hdrSetDoWrite(int dw) { m_hdrwrite=!!dw; }
  148. int hdrGetTOCSize() { return m_hdrtoc; }
  149. int hdrSetTOCSize(int ts) { m_hdrtoc=ts; }
  150. int ConfigUI(HINSTANCE hInstance, HWND hwndParent);
  151. private:
  152. void FlushOut();
  153. void ReadIn();
  154. static BOOL CALLBACK _dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam);
  155. BOOL dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam);
  156. HWND audio_hwnd, video_hwnd;
  157. void initConfigChildWindows(HWND hwndDlg, int flags);
  158. char *m_filename;
  159. unsigned int m_audfmt,m_vidfmt;
  160. int m_minsync, m_maxsync;
  161. int m_audiosendahead;
  162. int m_hdrwrite;
  163. int m_hdrtoc;
  164. char *m_metadata;
  165. public:
  166. int m_crop_l; // ##
  167. int m_crop_r; // ##
  168. int m_crop_t; // ##
  169. int m_crop_b; // ##
  170. };
  171. #endif//_NSVENCODE_H_