src/http/modules/ngx_http_mp4_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */

  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. #define NGX_HTTP_MP4_TRAK_ATOM     0
  9. #define NGX_HTTP_MP4_TKHD_ATOM     1
  10. #define NGX_HTTP_MP4_MDIA_ATOM     2
  11. #define NGX_HTTP_MP4_MDHD_ATOM     3
  12. #define NGX_HTTP_MP4_HDLR_ATOM     4
  13. #define NGX_HTTP_MP4_MINF_ATOM     5
  14. #define NGX_HTTP_MP4_VMHD_ATOM     6
  15. #define NGX_HTTP_MP4_SMHD_ATOM     7
  16. #define NGX_HTTP_MP4_DINF_ATOM     8
  17. #define NGX_HTTP_MP4_STBL_ATOM     9
  18. #define NGX_HTTP_MP4_STSD_ATOM    10
  19. #define NGX_HTTP_MP4_STTS_ATOM    11
  20. #define NGX_HTTP_MP4_STTS_DATA    12
  21. #define NGX_HTTP_MP4_STSS_ATOM    13
  22. #define NGX_HTTP_MP4_STSS_DATA    14
  23. #define NGX_HTTP_MP4_CTTS_ATOM    15
  24. #define NGX_HTTP_MP4_CTTS_DATA    16
  25. #define NGX_HTTP_MP4_STSC_ATOM    17
  26. #define NGX_HTTP_MP4_STSC_START   18
  27. #define NGX_HTTP_MP4_STSC_DATA    19
  28. #define NGX_HTTP_MP4_STSC_END     20
  29. #define NGX_HTTP_MP4_STSZ_ATOM    21
  30. #define NGX_HTTP_MP4_STSZ_DATA    22
  31. #define NGX_HTTP_MP4_STCO_ATOM    23
  32. #define NGX_HTTP_MP4_STCO_DATA    24
  33. #define NGX_HTTP_MP4_CO64_ATOM    25
  34. #define NGX_HTTP_MP4_CO64_DATA    26

  35. #define NGX_HTTP_MP4_LAST_ATOM    NGX_HTTP_MP4_CO64_DATA


  36. typedef struct {
  37.     size_t                buffer_size;
  38.     size_t                max_buffer_size;
  39. } ngx_http_mp4_conf_t;


  40. typedef struct {
  41.     u_char                chunk[4];
  42.     u_char                samples[4];
  43.     u_char                id[4];
  44. } ngx_mp4_stsc_entry_t;


  45. typedef struct {
  46.     uint32_t              timescale;
  47.     uint32_t              time_to_sample_entries;
  48.     uint32_t              sample_to_chunk_entries;
  49.     uint32_t              sync_samples_entries;
  50.     uint32_t              composition_offset_entries;
  51.     uint32_t              sample_sizes_entries;
  52.     uint32_t              chunks;

  53.     ngx_uint_t            start_sample;
  54.     ngx_uint_t            end_sample;
  55.     ngx_uint_t            start_chunk;
  56.     ngx_uint_t            end_chunk;
  57.     ngx_uint_t            start_chunk_samples;
  58.     ngx_uint_t            end_chunk_samples;
  59.     uint64_t              start_chunk_samples_size;
  60.     uint64_t              end_chunk_samples_size;
  61.     off_t                 start_offset;
  62.     off_t                 end_offset;

  63.     size_t                tkhd_size;
  64.     size_t                mdhd_size;
  65.     size_t                hdlr_size;
  66.     size_t                vmhd_size;
  67.     size_t                smhd_size;
  68.     size_t                dinf_size;
  69.     size_t                size;

  70.     ngx_chain_t           out[NGX_HTTP_MP4_LAST_ATOM + 1];

  71.     ngx_buf_t             trak_atom_buf;
  72.     ngx_buf_t             tkhd_atom_buf;
  73.     ngx_buf_t             mdia_atom_buf;
  74.     ngx_buf_t             mdhd_atom_buf;
  75.     ngx_buf_t             hdlr_atom_buf;
  76.     ngx_buf_t             minf_atom_buf;
  77.     ngx_buf_t             vmhd_atom_buf;
  78.     ngx_buf_t             smhd_atom_buf;
  79.     ngx_buf_t             dinf_atom_buf;
  80.     ngx_buf_t             stbl_atom_buf;
  81.     ngx_buf_t             stsd_atom_buf;
  82.     ngx_buf_t             stts_atom_buf;
  83.     ngx_buf_t             stts_data_buf;
  84.     ngx_buf_t             stss_atom_buf;
  85.     ngx_buf_t             stss_data_buf;
  86.     ngx_buf_t             ctts_atom_buf;
  87.     ngx_buf_t             ctts_data_buf;
  88.     ngx_buf_t             stsc_atom_buf;
  89.     ngx_buf_t             stsc_start_chunk_buf;
  90.     ngx_buf_t             stsc_end_chunk_buf;
  91.     ngx_buf_t             stsc_data_buf;
  92.     ngx_buf_t             stsz_atom_buf;
  93.     ngx_buf_t             stsz_data_buf;
  94.     ngx_buf_t             stco_atom_buf;
  95.     ngx_buf_t             stco_data_buf;
  96.     ngx_buf_t             co64_atom_buf;
  97.     ngx_buf_t             co64_data_buf;

  98.     ngx_mp4_stsc_entry_t  stsc_start_chunk_entry;
  99.     ngx_mp4_stsc_entry_t  stsc_end_chunk_entry;
  100. } ngx_http_mp4_trak_t;


  101. typedef struct {
  102.     ngx_file_t            file;

  103.     u_char               *buffer;
  104.     u_char               *buffer_start;
  105.     u_char               *buffer_pos;
  106.     u_char               *buffer_end;
  107.     size_t                buffer_size;

  108.     off_t                 offset;
  109.     off_t                 end;
  110.     off_t                 content_length;
  111.     ngx_uint_t            start;
  112.     ngx_uint_t            length;
  113.     uint32_t              timescale;
  114.     ngx_http_request_t   *request;
  115.     ngx_array_t           trak;
  116.     ngx_http_mp4_trak_t   traks[2];

  117.     size_t                ftyp_size;
  118.     size_t                moov_size;

  119.     ngx_chain_t          *out;
  120.     ngx_chain_t           ftyp_atom;
  121.     ngx_chain_t           moov_atom;
  122.     ngx_chain_t           mvhd_atom;
  123.     ngx_chain_t           mdat_atom;
  124.     ngx_chain_t           mdat_data;

  125.     ngx_buf_t             ftyp_atom_buf;
  126.     ngx_buf_t             moov_atom_buf;
  127.     ngx_buf_t             mvhd_atom_buf;
  128.     ngx_buf_t             mdat_atom_buf;
  129.     ngx_buf_t             mdat_data_buf;

  130.     u_char                moov_atom_header[8];
  131.     u_char                mdat_atom_header[16];
  132. } ngx_http_mp4_file_t;


  133. typedef struct {
  134.     char                 *name;
  135.     ngx_int_t           (*handler)(ngx_http_mp4_file_t *mp4,
  136.                                    uint64_t atom_data_size);
  137. } ngx_http_mp4_atom_handler_t;


  138. #define ngx_mp4_atom_header(mp4)   (mp4->buffer_pos - 8)
  139. #define ngx_mp4_atom_data(mp4)     mp4->buffer_pos
  140. #define ngx_mp4_atom_data_size(t)  (uint64_t) (sizeof(t) - 8)


  141. #define ngx_mp4_atom_next(mp4, n)                                             \
  142.     mp4->buffer_pos += (size_t) n;                                            \
  143.     mp4->offset += n


  144. #define ngx_mp4_set_atom_name(p, n1, n2, n3, n4)                              \
  145.     ((u_char *) (p))[4] = n1;                                                 \
  146.     ((u_char *) (p))[5] = n2;                                                 \
  147.     ((u_char *) (p))[6] = n3;                                                 \
  148.     ((u_char *) (p))[7] = n4

  149. #define ngx_mp4_get_32value(p)                                                \
  150.     ( ((uint32_t) ((u_char *) (p))[0] << 24)                                  \
  151.     + (           ((u_char *) (p))[1] << 16)                                  \
  152.     + (           ((u_char *) (p))[2] << 8)                                   \
  153.     + (           ((u_char *) (p))[3]) )

  154. #define ngx_mp4_set_32value(p, n)                                             \
  155.     ((u_char *) (p))[0] = (u_char) ((n) >> 24);                               \
  156.     ((u_char *) (p))[1] = (u_char) ((n) >> 16);                               \
  157.     ((u_char *) (p))[2] = (u_char) ((n) >> 8);                                \
  158.     ((u_char *) (p))[3] = (u_char)  (n)

  159. #define ngx_mp4_get_64value(p)                                                \
  160.     ( ((uint64_t) ((u_char *) (p))[0] << 56)                                  \
  161.     + ((uint64_t) ((u_char *) (p))[1] << 48)                                  \
  162.     + ((uint64_t) ((u_char *) (p))[2] << 40)                                  \
  163.     + ((uint64_t) ((u_char *) (p))[3] << 32)                                  \
  164.     + ((uint64_t) ((u_char *) (p))[4] << 24)                                  \
  165.     + (           ((u_char *) (p))[5] << 16)                                  \
  166.     + (           ((u_char *) (p))[6] << 8)                                   \
  167.     + (           ((u_char *) (p))[7]) )

  168. #define ngx_mp4_set_64value(p, n)                                             \
  169.     ((u_char *) (p))[0] = (u_char) ((uint64_t) (n) >> 56);                    \
  170.     ((u_char *) (p))[1] = (u_char) ((uint64_t) (n) >> 48);                    \
  171.     ((u_char *) (p))[2] = (u_char) ((uint64_t) (n) >> 40);                    \
  172.     ((u_char *) (p))[3] = (u_char) ((uint64_t) (n) >> 32);                    \
  173.     ((u_char *) (p))[4] = (u_char) (           (n) >> 24);                    \
  174.     ((u_char *) (p))[5] = (u_char) (           (n) >> 16);                    \
  175.     ((u_char *) (p))[6] = (u_char) (           (n) >> 8);                     \
  176.     ((u_char *) (p))[7] = (u_char)             (n)

  177. #define ngx_mp4_last_trak(mp4)                                                \
  178.     &((ngx_http_mp4_trak_t *) mp4->trak.elts)[mp4->trak.nelts - 1]


  179. static ngx_int_t ngx_http_mp4_handler(ngx_http_request_t *r);

  180. static ngx_int_t ngx_http_mp4_process(ngx_http_mp4_file_t *mp4);
  181. static ngx_int_t ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
  182.     ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size);
  183. static ngx_int_t ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size);
  184. static ngx_int_t ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4,
  185.     uint64_t atom_data_size);
  186. static ngx_int_t ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4,
  187.     uint64_t atom_data_size);
  188. static ngx_int_t ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4,
  189.     uint64_t atom_data_size);
  190. static size_t ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4,
  191.     off_t start_offset, off_t end_offset);
  192. static ngx_int_t ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4,
  193.     uint64_t atom_data_size);
  194. static ngx_int_t ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4,
  195.     uint64_t atom_data_size);
  196. static void ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
  197.     ngx_http_mp4_trak_t *trak);
  198. static ngx_int_t ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4,
  199.     uint64_t atom_data_size);
  200. static ngx_int_t ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4,
  201.     uint64_t atom_data_size);
  202. static ngx_int_t ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4,
  203.     uint64_t atom_data_size);
  204. static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
  205.     ngx_http_mp4_trak_t *trak);
  206. static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4,
  207.     uint64_t atom_data_size);
  208. static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4,
  209.     uint64_t atom_data_size);
  210. static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4,
  211.     uint64_t atom_data_size);
  212. static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
  213.     ngx_http_mp4_trak_t *trak);
  214. static ngx_int_t ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4,
  215.     uint64_t atom_data_size);
  216. static ngx_int_t ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4,
  217.     uint64_t atom_data_size);
  218. static ngx_int_t ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4,
  219.     uint64_t atom_data_size);
  220. static ngx_int_t ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4,
  221.     uint64_t atom_data_size);
  222. static void ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
  223.     ngx_http_mp4_trak_t *trak);
  224. static ngx_int_t ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4,
  225.     uint64_t atom_data_size);
  226. static ngx_int_t ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4,
  227.     uint64_t atom_data_size);
  228. static ngx_int_t ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
  229.     ngx_http_mp4_trak_t *trak);
  230. static ngx_int_t ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
  231.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  232. static ngx_int_t ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4,
  233.     uint64_t atom_data_size);
  234. static ngx_int_t ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
  235.     ngx_http_mp4_trak_t *trak);
  236. static void ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
  237.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  238. static ngx_int_t ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4,
  239.     uint64_t atom_data_size);
  240. static void ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
  241.     ngx_http_mp4_trak_t *trak);
  242. static void ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
  243.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  244. static ngx_int_t ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4,
  245.     uint64_t atom_data_size);
  246. static ngx_int_t ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
  247.     ngx_http_mp4_trak_t *trak);
  248. static ngx_int_t ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
  249.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  250. static ngx_int_t ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4,
  251.     uint64_t atom_data_size);
  252. static ngx_int_t ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
  253.     ngx_http_mp4_trak_t *trak);
  254. static ngx_int_t ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4,
  255.     uint64_t atom_data_size);
  256. static ngx_int_t ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
  257.     ngx_http_mp4_trak_t *trak);
  258. static void ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
  259.     ngx_http_mp4_trak_t *trak, int32_t adjustment);
  260. static ngx_int_t ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4,
  261.     uint64_t atom_data_size);
  262. static ngx_int_t ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
  263.     ngx_http_mp4_trak_t *trak);
  264. static void ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4,
  265.     ngx_http_mp4_trak_t *trak, off_t adjustment);

  266. static char *ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  267. static void *ngx_http_mp4_create_conf(ngx_conf_t *cf);
  268. static char *ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child);


  269. static ngx_command_t  ngx_http_mp4_commands[] = {

  270.     { ngx_string("mp4"),
  271.       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
  272.       ngx_http_mp4,
  273.       0,
  274.       0,
  275.       NULL },

  276.     { ngx_string("mp4_buffer_size"),
  277.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  278.       ngx_conf_set_size_slot,
  279.       NGX_HTTP_LOC_CONF_OFFSET,
  280.       offsetof(ngx_http_mp4_conf_t, buffer_size),
  281.       NULL },

  282.     { ngx_string("mp4_max_buffer_size"),
  283.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  284.       ngx_conf_set_size_slot,
  285.       NGX_HTTP_LOC_CONF_OFFSET,
  286.       offsetof(ngx_http_mp4_conf_t, max_buffer_size),
  287.       NULL },

  288.       ngx_null_command
  289. };


  290. static ngx_http_module_t  ngx_http_mp4_module_ctx = {
  291.     NULL,                          /* preconfiguration */
  292.     NULL,                          /* postconfiguration */

  293.     NULL,                          /* create main configuration */
  294.     NULL,                          /* init main configuration */

  295.     NULL,                          /* create server configuration */
  296.     NULL,                          /* merge server configuration */

  297.     ngx_http_mp4_create_conf,      /* create location configuration */
  298.     ngx_http_mp4_merge_conf        /* merge location configuration */
  299. };


  300. ngx_module_t  ngx_http_mp4_module = {
  301.     NGX_MODULE_V1,
  302.     &ngx_http_mp4_module_ctx,      /* module context */
  303.     ngx_http_mp4_commands,         /* module directives */
  304.     NGX_HTTP_MODULE,               /* module type */
  305.     NULL,                          /* init master */
  306.     NULL,                          /* init module */
  307.     NULL,                          /* init process */
  308.     NULL,                          /* init thread */
  309.     NULL,                          /* exit thread */
  310.     NULL,                          /* exit process */
  311.     NULL,                          /* exit master */
  312.     NGX_MODULE_V1_PADDING
  313. };


  314. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_atoms[] = {
  315.     { "ftyp", ngx_http_mp4_read_ftyp_atom },
  316.     { "moov", ngx_http_mp4_read_moov_atom },
  317.     { "mdat", ngx_http_mp4_read_mdat_atom },
  318.     { NULL, NULL }
  319. };

  320. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_moov_atoms[] = {
  321.     { "mvhd", ngx_http_mp4_read_mvhd_atom },
  322.     { "trak", ngx_http_mp4_read_trak_atom },
  323.     { "cmov", ngx_http_mp4_read_cmov_atom },
  324.     { NULL, NULL }
  325. };

  326. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_trak_atoms[] = {
  327.     { "tkhd", ngx_http_mp4_read_tkhd_atom },
  328.     { "mdia", ngx_http_mp4_read_mdia_atom },
  329.     { NULL, NULL }
  330. };

  331. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_mdia_atoms[] = {
  332.     { "mdhd", ngx_http_mp4_read_mdhd_atom },
  333.     { "hdlr", ngx_http_mp4_read_hdlr_atom },
  334.     { "minf", ngx_http_mp4_read_minf_atom },
  335.     { NULL, NULL }
  336. };

  337. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_minf_atoms[] = {
  338.     { "vmhd", ngx_http_mp4_read_vmhd_atom },
  339.     { "smhd", ngx_http_mp4_read_smhd_atom },
  340.     { "dinf", ngx_http_mp4_read_dinf_atom },
  341.     { "stbl", ngx_http_mp4_read_stbl_atom },
  342.     { NULL, NULL }
  343. };

  344. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_stbl_atoms[] = {
  345.     { "stsd", ngx_http_mp4_read_stsd_atom },
  346.     { "stts", ngx_http_mp4_read_stts_atom },
  347.     { "stss", ngx_http_mp4_read_stss_atom },
  348.     { "ctts", ngx_http_mp4_read_ctts_atom },
  349.     { "stsc", ngx_http_mp4_read_stsc_atom },
  350.     { "stsz", ngx_http_mp4_read_stsz_atom },
  351.     { "stco", ngx_http_mp4_read_stco_atom },
  352.     { "co64", ngx_http_mp4_read_co64_atom },
  353.     { NULL, NULL }
  354. };


  355. static ngx_int_t
  356. ngx_http_mp4_handler(ngx_http_request_t *r)
  357. {
  358.     u_char                    *last;
  359.     size_t                     root;
  360.     ngx_int_t                  rc, start, end;
  361.     ngx_uint_t                 level, length;
  362.     ngx_str_t                  path, value;
  363.     ngx_log_t                 *log;
  364.     ngx_buf_t                 *b;
  365.     ngx_chain_t                out;
  366.     ngx_http_mp4_file_t       *mp4;
  367.     ngx_open_file_info_t       of;
  368.     ngx_http_core_loc_conf_t  *clcf;

  369.     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
  370.         return NGX_HTTP_NOT_ALLOWED;
  371.     }

  372.     if (r->uri.data[r->uri.len - 1] == '/') {
  373.         return NGX_DECLINED;
  374.     }

  375.     rc = ngx_http_discard_request_body(r);

  376.     if (rc != NGX_OK) {
  377.         return rc;
  378.     }

  379.     last = ngx_http_map_uri_to_path(r, &path, &root, 0);
  380.     if (last == NULL) {
  381.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  382.     }

  383.     log = r->connection->log;

  384.     path.len = last - path.data;

  385.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
  386.                    "http mp4 filename: \"%V\"", &path);

  387.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  388.     ngx_memzero(&of, sizeof(ngx_open_file_info_t));

  389.     of.read_ahead = clcf->read_ahead;
  390.     of.directio = NGX_MAX_OFF_T_VALUE;
  391.     of.valid = clcf->open_file_cache_valid;
  392.     of.min_uses = clcf->open_file_cache_min_uses;
  393.     of.errors = clcf->open_file_cache_errors;
  394.     of.events = clcf->open_file_cache_events;

  395.     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
  396.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  397.     }

  398.     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
  399.         != NGX_OK)
  400.     {
  401.         switch (of.err) {

  402.         case 0:
  403.             return NGX_HTTP_INTERNAL_SERVER_ERROR;

  404.         case NGX_ENOENT:
  405.         case NGX_ENOTDIR:
  406.         case NGX_ENAMETOOLONG:

  407.             level = NGX_LOG_ERR;
  408.             rc = NGX_HTTP_NOT_FOUND;
  409.             break;

  410.         case NGX_EACCES:
  411. #if (NGX_HAVE_OPENAT)
  412.         case NGX_EMLINK:
  413.         case NGX_ELOOP:
  414. #endif

  415.             level = NGX_LOG_ERR;
  416.             rc = NGX_HTTP_FORBIDDEN;
  417.             break;

  418.         default:

  419.             level = NGX_LOG_CRIT;
  420.             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
  421.             break;
  422.         }

  423.         if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
  424.             ngx_log_error(level, log, of.err,
  425.                           "%s \"%s\" failed", of.failed, path.data);
  426.         }

  427.         return rc;
  428.     }

  429.     if (!of.is_file) {

  430.         if (ngx_close_file(of.fd) == NGX_FILE_ERROR) {
  431.             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  432.                           ngx_close_file_n " \"%s\" failed", path.data);
  433.         }

  434.         return NGX_DECLINED;
  435.     }

  436.     r->root_tested = !r->error_page;
  437.     r->allow_ranges = 1;

  438.     start = -1;
  439.     length = 0;
  440.     r->headers_out.content_length_n = of.size;
  441.     mp4 = NULL;
  442.     b = NULL;

  443.     if (r->args.len) {

  444.         if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {

  445.             /*
  446.              * A Flash player may send start value with a lot of digits
  447.              * after dot so strtod() is used instead of atofp().  NaNs and
  448.              * infinities become negative numbers after (int) conversion.
  449.              */

  450.             ngx_set_errno(0);
  451.             start = (int) (strtod((char *) value.data, NULL) * 1000);

  452.             if (ngx_errno != 0) {
  453.                 start = -1;
  454.             }
  455.         }

  456.         if (ngx_http_arg(r, (u_char *) "end", 3, &value) == NGX_OK) {

  457.             ngx_set_errno(0);
  458.             end = (int) (strtod((char *) value.data, NULL) * 1000);

  459.             if (ngx_errno != 0) {
  460.                 end = -1;
  461.             }

  462.             if (end > 0) {
  463.                 if (start < 0) {
  464.                     start = 0;
  465.                 }

  466.                 if (end > start) {
  467.                     length = end - start;
  468.                 }
  469.             }
  470.         }
  471.     }

  472.     if (start >= 0) {
  473.         r->single_range = 1;

  474.         mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
  475.         if (mp4 == NULL) {
  476.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  477.         }

  478.         mp4->file.fd = of.fd;
  479.         mp4->file.name = path;
  480.         mp4->file.log = r->connection->log;
  481.         mp4->end = of.size;
  482.         mp4->start = (ngx_uint_t) start;
  483.         mp4->length = length;
  484.         mp4->request = r;

  485.         switch (ngx_http_mp4_process(mp4)) {

  486.         case NGX_DECLINED:
  487.             if (mp4->buffer) {
  488.                 ngx_pfree(r->pool, mp4->buffer);
  489.             }

  490.             ngx_pfree(r->pool, mp4);
  491.             mp4 = NULL;

  492.             break;

  493.         case NGX_OK:
  494.             r->headers_out.content_length_n = mp4->content_length;
  495.             break;

  496.         default: /* NGX_ERROR */
  497.             if (mp4->buffer) {
  498.                 ngx_pfree(r->pool, mp4->buffer);
  499.             }

  500.             ngx_pfree(r->pool, mp4);

  501.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  502.         }
  503.     }

  504.     log->action = "sending mp4 to client";

  505.     if (clcf->directio <= of.size) {

  506.         /*
  507.          * DIRECTIO is set on transfer only
  508.          * to allow kernel to cache "moov" atom
  509.          */

  510.         if (ngx_directio_on(of.fd) == NGX_FILE_ERROR) {
  511.             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  512.                           ngx_directio_on_n " \"%s\" failed", path.data);
  513.         }

  514.         of.is_directio = 1;

  515.         if (mp4) {
  516.             mp4->file.directio = 1;
  517.         }
  518.     }

  519.     r->headers_out.status = NGX_HTTP_OK;
  520.     r->headers_out.last_modified_time = of.mtime;

  521.     if (ngx_http_set_etag(r) != NGX_OK) {
  522.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  523.     }

  524.     if (ngx_http_set_content_type(r) != NGX_OK) {
  525.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  526.     }

  527.     if (mp4 == NULL) {
  528.         b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
  529.         if (b == NULL) {
  530.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  531.         }

  532.         b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
  533.         if (b->file == NULL) {
  534.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  535.         }
  536.     }

  537.     rc = ngx_http_send_header(r);

  538.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  539.         return rc;
  540.     }

  541.     if (mp4) {
  542.         return ngx_http_output_filter(r, mp4->out);
  543.     }

  544.     b->file_pos = 0;
  545.     b->file_last = of.size;

  546.     b->in_file = b->file_last ? 1 : 0;
  547.     b->last_buf = (r == r->main) ? 1 : 0;
  548.     b->last_in_chain = 1;

  549.     b->file->fd = of.fd;
  550.     b->file->name = path;
  551.     b->file->log = log;
  552.     b->file->directio = of.is_directio;

  553.     out.buf = b;
  554.     out.next = NULL;

  555.     return ngx_http_output_filter(r, &out);
  556. }


  557. static ngx_int_t
  558. ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
  559. {
  560.     off_t                  start_offset, end_offset, adjustment;
  561.     ngx_int_t              rc;
  562.     ngx_uint_t             i, j;
  563.     ngx_chain_t          **prev;
  564.     ngx_http_mp4_trak_t   *trak;
  565.     ngx_http_mp4_conf_t   *conf;

  566.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  567.                    "mp4 start:%ui, length:%ui", mp4->start, mp4->length);

  568.     conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);

  569.     mp4->buffer_size = conf->buffer_size;

  570.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_atoms, mp4->end);
  571.     if (rc != NGX_OK) {
  572.         return rc;
  573.     }

  574.     if (mp4->trak.nelts == 0) {
  575.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  576.                       "no mp4 trak atoms were found in \"%s\"",
  577.                       mp4->file.name.data);
  578.         return NGX_ERROR;
  579.     }

  580.     if (mp4->mdat_atom.buf == NULL) {
  581.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  582.                       "no mp4 mdat atom was found in \"%s\"",
  583.                       mp4->file.name.data);
  584.         return NGX_ERROR;
  585.     }

  586.     prev = &mp4->out;

  587.     if (mp4->ftyp_atom.buf) {
  588.         *prev = &mp4->ftyp_atom;
  589.         prev = &mp4->ftyp_atom.next;
  590.     }

  591.     *prev = &mp4->moov_atom;
  592.     prev = &mp4->moov_atom.next;

  593.     if (mp4->mvhd_atom.buf) {
  594.         mp4->moov_size += mp4->mvhd_atom_buf.last - mp4->mvhd_atom_buf.pos;
  595.         *prev = &mp4->mvhd_atom;
  596.         prev = &mp4->mvhd_atom.next;
  597.     }

  598.     start_offset = mp4->end;
  599.     end_offset = 0;
  600.     trak = mp4->trak.elts;

  601.     for (i = 0; i < mp4->trak.nelts; i++) {

  602.         if (ngx_http_mp4_update_stts_atom(mp4, &trak[i]) != NGX_OK) {
  603.             return NGX_ERROR;
  604.         }

  605.         if (ngx_http_mp4_update_stss_atom(mp4, &trak[i]) != NGX_OK) {
  606.             return NGX_ERROR;
  607.         }

  608.         ngx_http_mp4_update_ctts_atom(mp4, &trak[i]);

  609.         if (ngx_http_mp4_update_stsc_atom(mp4, &trak[i]) != NGX_OK) {
  610.             return NGX_ERROR;
  611.         }

  612.         if (ngx_http_mp4_update_stsz_atom(mp4, &trak[i]) != NGX_OK) {
  613.             return NGX_ERROR;
  614.         }

  615.         if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
  616.             if (ngx_http_mp4_update_co64_atom(mp4, &trak[i]) != NGX_OK) {
  617.                 return NGX_ERROR;
  618.             }

  619.         } else {
  620.             if (ngx_http_mp4_update_stco_atom(mp4, &trak[i]) != NGX_OK) {
  621.                 return NGX_ERROR;
  622.             }
  623.         }

  624.         ngx_http_mp4_update_stbl_atom(mp4, &trak[i]);
  625.         ngx_http_mp4_update_minf_atom(mp4, &trak[i]);
  626.         trak[i].size += trak[i].mdhd_size;
  627.         trak[i].size += trak[i].hdlr_size;
  628.         ngx_http_mp4_update_mdia_atom(mp4, &trak[i]);
  629.         trak[i].size += trak[i].tkhd_size;
  630.         ngx_http_mp4_update_trak_atom(mp4, &trak[i]);

  631.         mp4->moov_size += trak[i].size;

  632.         if (start_offset > trak[i].start_offset) {
  633.             start_offset = trak[i].start_offset;
  634.         }

  635.         if (end_offset < trak[i].end_offset) {
  636.             end_offset = trak[i].end_offset;
  637.         }

  638.         *prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM];
  639.         prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next;

  640.         for (j = 0; j < NGX_HTTP_MP4_LAST_ATOM + 1; j++) {
  641.             if (trak[i].out[j].buf) {
  642.                 *prev = &trak[i].out[j];
  643.                 prev = &trak[i].out[j].next;
  644.             }
  645.         }
  646.     }

  647.     if (end_offset < start_offset) {
  648.         end_offset = start_offset;
  649.     }

  650.     mp4->moov_size += 8;

  651.     ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size);
  652.     ngx_mp4_set_atom_name(mp4->moov_atom_header, 'm', 'o', 'o', 'v');
  653.     mp4->content_length += mp4->moov_size;

  654.     *prev = &mp4->mdat_atom;

  655.     if (start_offset > mp4->mdat_data.buf->file_last) {
  656.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  657.                       "start time is out mp4 mdat atom in \"%s\"",
  658.                       mp4->file.name.data);
  659.         return NGX_ERROR;
  660.     }

  661.     adjustment = mp4->ftyp_size + mp4->moov_size
  662.                  + ngx_http_mp4_update_mdat_atom(mp4, start_offset, end_offset)
  663.                  - start_offset;

  664.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  665.                    "mp4 adjustment:%O", adjustment);

  666.     for (i = 0; i < mp4->trak.nelts; i++) {
  667.         if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
  668.             ngx_http_mp4_adjust_co64_atom(mp4, &trak[i], adjustment);
  669.         } else {
  670.             ngx_http_mp4_adjust_stco_atom(mp4, &trak[i], (int32_t) adjustment);
  671.         }
  672.     }

  673.     return NGX_OK;
  674. }


  675. typedef struct {
  676.     u_char    size[4];
  677.     u_char    name[4];
  678. } ngx_mp4_atom_header_t;

  679. typedef struct {
  680.     u_char    size[4];
  681.     u_char    name[4];
  682.     u_char    size64[8];
  683. } ngx_mp4_atom_header64_t;


  684. static ngx_int_t
  685. ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
  686.     ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size)
  687. {
  688.     off_t        end;
  689.     size_t       atom_header_size;
  690.     u_char      *atom_header, *atom_name;
  691.     uint64_t     atom_size;
  692.     ngx_int_t    rc;
  693.     ngx_uint_t   n;

  694.     end = mp4->offset + atom_data_size;

  695.     while (mp4->offset < end) {

  696.         if (ngx_http_mp4_read(mp4, sizeof(uint32_t)) != NGX_OK) {
  697.             return NGX_ERROR;
  698.         }

  699.         atom_header = mp4->buffer_pos;
  700.         atom_size = ngx_mp4_get_32value(atom_header);
  701.         atom_header_size = sizeof(ngx_mp4_atom_header_t);

  702.         if (atom_size == 0) {
  703.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  704.                            "mp4 atom end");
  705.             return NGX_OK;
  706.         }

  707.         if (atom_size < sizeof(ngx_mp4_atom_header_t)) {

  708.             if (atom_size == 1) {

  709.                 if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header64_t))
  710.                     != NGX_OK)
  711.                 {
  712.                     return NGX_ERROR;
  713.                 }

  714.                 /* 64-bit atom size */
  715.                 atom_header = mp4->buffer_pos;
  716.                 atom_size = ngx_mp4_get_64value(atom_header + 8);
  717.                 atom_header_size = sizeof(ngx_mp4_atom_header64_t);

  718.             } else {
  719.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  720.                               "\"%s\" mp4 atom is too small:%uL",
  721.                               mp4->file.name.data, atom_size);
  722.                 return NGX_ERROR;
  723.             }
  724.         }

  725.         if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header_t)) != NGX_OK) {
  726.             return NGX_ERROR;
  727.         }

  728.         atom_header = mp4->buffer_pos;
  729.         atom_name = atom_header + sizeof(uint32_t);

  730.         ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  731.                        "mp4 atom: %*s @%O:%uL",
  732.                        4, atom_name, mp4->offset, atom_size);

  733.         if (atom_size > (uint64_t) (NGX_MAX_OFF_T_VALUE - mp4->offset)
  734.             || mp4->offset + (off_t) atom_size > end)
  735.         {
  736.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  737.                           "\"%s\" mp4 atom too large:%uL",
  738.                           mp4->file.name.data, atom_size);
  739.             return NGX_ERROR;
  740.         }

  741.         for (n = 0; atom[n].name; n++) {

  742.             if (ngx_strncmp(atom_name, atom[n].name, 4) == 0) {

  743.                 ngx_mp4_atom_next(mp4, atom_header_size);

  744.                 rc = atom[n].handler(mp4, atom_size - atom_header_size);
  745.                 if (rc != NGX_OK) {
  746.                     return rc;
  747.                 }

  748.                 goto next;
  749.             }
  750.         }

  751.         ngx_mp4_atom_next(mp4, atom_size);

  752.     next:
  753.         continue;
  754.     }

  755.     return NGX_OK;
  756. }


  757. static ngx_int_t
  758. ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size)
  759. {
  760.     ssize_t  n;

  761.     if (mp4->buffer_pos + size <= mp4->buffer_end) {
  762.         return NGX_OK;
  763.     }

  764.     if (mp4->offset + (off_t) mp4->buffer_size > mp4->end) {
  765.         mp4->buffer_size = (size_t) (mp4->end - mp4->offset);
  766.     }

  767.     if (mp4->buffer_size < size) {
  768.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  769.                       "\"%s\" mp4 file truncated", mp4->file.name.data);
  770.         return NGX_ERROR;
  771.     }

  772.     if (mp4->buffer == NULL) {
  773.         mp4->buffer = ngx_palloc(mp4->request->pool, mp4->buffer_size);
  774.         if (mp4->buffer == NULL) {
  775.             return NGX_ERROR;
  776.         }

  777.         mp4->buffer_start = mp4->buffer;
  778.     }

  779.     n = ngx_read_file(&mp4->file, mp4->buffer_start, mp4->buffer_size,
  780.                       mp4->offset);

  781.     if (n == NGX_ERROR) {
  782.         return NGX_ERROR;
  783.     }

  784.     if ((size_t) n != mp4->buffer_size) {
  785.         ngx_log_error(NGX_LOG_CRIT, mp4->file.log, 0,
  786.                       ngx_read_file_n " read only %z of %z from \"%s\"",
  787.                       n, mp4->buffer_size, mp4->file.name.data);
  788.         return NGX_ERROR;
  789.     }

  790.     mp4->buffer_pos = mp4->buffer_start;
  791.     mp4->buffer_end = mp4->buffer_start + mp4->buffer_size;

  792.     return NGX_OK;
  793. }


  794. static ngx_int_t
  795. ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  796. {
  797.     u_char     *ftyp_atom;
  798.     size_t      atom_size;
  799.     ngx_buf_t  *atom;

  800.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ftyp atom");

  801.     if (atom_data_size > 1024
  802.         || ngx_mp4_atom_data(mp4) + (size_t) atom_data_size > mp4->buffer_end)
  803.     {
  804.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  805.                       "\"%s\" mp4 ftyp atom is too large:%uL",
  806.                       mp4->file.name.data, atom_data_size);
  807.         return NGX_ERROR;
  808.     }

  809.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  810.     ftyp_atom = ngx_palloc(mp4->request->pool, atom_size);
  811.     if (ftyp_atom == NULL) {
  812.         return NGX_ERROR;
  813.     }

  814.     ngx_mp4_set_32value(ftyp_atom, atom_size);
  815.     ngx_mp4_set_atom_name(ftyp_atom, 'f', 't', 'y', 'p');

  816.     /*
  817.      * only moov atom content is guaranteed to be in mp4->buffer
  818.      * during sending response, so ftyp atom content should be copied
  819.      */
  820.     ngx_memcpy(ftyp_atom + sizeof(ngx_mp4_atom_header_t),
  821.                ngx_mp4_atom_data(mp4), (size_t) atom_data_size);

  822.     atom = &mp4->ftyp_atom_buf;
  823.     atom->temporary = 1;
  824.     atom->pos = ftyp_atom;
  825.     atom->last = ftyp_atom + atom_size;

  826.     mp4->ftyp_atom.buf = atom;
  827.     mp4->ftyp_size = atom_size;
  828.     mp4->content_length = atom_size;

  829.     ngx_mp4_atom_next(mp4, atom_data_size);

  830.     return NGX_OK;
  831. }


  832. /*
  833. * Small excess buffer to process atoms after moov atom, mp4->buffer_start
  834. * will be set to this buffer part after moov atom processing.
  835. */
  836. #define NGX_HTTP_MP4_MOOV_BUFFER_EXCESS  (4 * 1024)

  837. static ngx_int_t
  838. ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  839. {
  840.     ngx_int_t             rc;
  841.     ngx_uint_t            no_mdat;
  842.     ngx_buf_t            *atom;
  843.     ngx_http_mp4_conf_t  *conf;

  844.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom");

  845.     no_mdat = (mp4->mdat_atom.buf == NULL);

  846.     if (no_mdat && mp4->start == 0 && mp4->length == 0) {
  847.         /*
  848.          * send original file if moov atom resides before
  849.          * mdat atom and client requests integral file
  850.          */
  851.         return NGX_DECLINED;
  852.     }

  853.     conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);

  854.     if (atom_data_size > mp4->buffer_size) {

  855.         if (atom_data_size > conf->max_buffer_size) {
  856.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  857.                           "\"%s\" mp4 moov atom is too large:%uL, "
  858.                           "you may want to increase mp4_max_buffer_size",
  859.                           mp4->file.name.data, atom_data_size);
  860.             return NGX_ERROR;
  861.         }

  862.         ngx_pfree(mp4->request->pool, mp4->buffer);
  863.         mp4->buffer = NULL;
  864.         mp4->buffer_pos = NULL;
  865.         mp4->buffer_end = NULL;

  866.         mp4->buffer_size = (size_t) atom_data_size
  867.                          + NGX_HTTP_MP4_MOOV_BUFFER_EXCESS * no_mdat;
  868.     }

  869.     if (ngx_http_mp4_read(mp4, (size_t) atom_data_size) != NGX_OK) {
  870.         return NGX_ERROR;
  871.     }

  872.     mp4->trak.elts = &mp4->traks;
  873.     mp4->trak.size = sizeof(ngx_http_mp4_trak_t);
  874.     mp4->trak.nalloc = 2;
  875.     mp4->trak.pool = mp4->request->pool;

  876.     atom = &mp4->moov_atom_buf;
  877.     atom->temporary = 1;
  878.     atom->pos = mp4->moov_atom_header;
  879.     atom->last = mp4->moov_atom_header + 8;

  880.     mp4->moov_atom.buf = &mp4->moov_atom_buf;

  881.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_moov_atoms, atom_data_size);

  882.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom done");

  883.     if (no_mdat) {
  884.         mp4->buffer_start = mp4->buffer_pos;
  885.         mp4->buffer_size = NGX_HTTP_MP4_MOOV_BUFFER_EXCESS;

  886.         if (mp4->buffer_start + mp4->buffer_size > mp4->buffer_end) {
  887.             mp4->buffer = NULL;
  888.             mp4->buffer_pos = NULL;
  889.             mp4->buffer_end = NULL;
  890.         }

  891.     } else {
  892.         /* skip atoms after moov atom */
  893.         mp4->offset = mp4->end;
  894.     }

  895.     return rc;
  896. }


  897. static ngx_int_t
  898. ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  899. {
  900.     ngx_buf_t  *data;

  901.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdat atom");

  902.     data = &mp4->mdat_data_buf;
  903.     data->file = &mp4->file;
  904.     data->in_file = 1;
  905.     data->last_buf = 1;
  906.     data->last_in_chain = 1;
  907.     data->file_last = mp4->offset + atom_data_size;

  908.     mp4->mdat_atom.buf = &mp4->mdat_atom_buf;
  909.     mp4->mdat_atom.next = &mp4->mdat_data;
  910.     mp4->mdat_data.buf = data;

  911.     if (mp4->trak.nelts) {
  912.         /* skip atoms after mdat atom */
  913.         mp4->offset = mp4->end;

  914.     } else {
  915.         ngx_mp4_atom_next(mp4, atom_data_size);
  916.     }

  917.     return NGX_OK;
  918. }


  919. static size_t
  920. ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset,
  921.     off_t end_offset)
  922. {
  923.     off_t       atom_data_size;
  924.     u_char     *atom_header;
  925.     uint32_t    atom_header_size;
  926.     uint64_t    atom_size;
  927.     ngx_buf_t  *atom;

  928.     atom_data_size = end_offset - start_offset;
  929.     mp4->mdat_data.buf->file_pos = start_offset;
  930.     mp4->mdat_data.buf->file_last = end_offset;

  931.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  932.                    "mdat new offset @%O:%O", start_offset, atom_data_size);

  933.     atom_header = mp4->mdat_atom_header;

  934.     if ((uint64_t) atom_data_size > (uint64_t) 0xffffffff) {
  935.         atom_size = 1;
  936.         atom_header_size = sizeof(ngx_mp4_atom_header64_t);
  937.         ngx_mp4_set_64value(atom_header + sizeof(ngx_mp4_atom_header_t),
  938.                             sizeof(ngx_mp4_atom_header64_t) + atom_data_size);
  939.     } else {
  940.         atom_size = sizeof(ngx_mp4_atom_header_t) + atom_data_size;
  941.         atom_header_size = sizeof(ngx_mp4_atom_header_t);
  942.     }

  943.     mp4->content_length += atom_header_size + atom_data_size;

  944.     ngx_mp4_set_32value(atom_header, atom_size);
  945.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'a', 't');

  946.     atom = &mp4->mdat_atom_buf;
  947.     atom->temporary = 1;
  948.     atom->pos = atom_header;
  949.     atom->last = atom_header + atom_header_size;

  950.     return atom_header_size;
  951. }


  952. typedef struct {
  953.     u_char    size[4];
  954.     u_char    name[4];
  955.     u_char    version[1];
  956.     u_char    flags[3];
  957.     u_char    creation_time[4];
  958.     u_char    modification_time[4];
  959.     u_char    timescale[4];
  960.     u_char    duration[4];
  961.     u_char    rate[4];
  962.     u_char    volume[2];
  963.     u_char    reserved[10];
  964.     u_char    matrix[36];
  965.     u_char    preview_time[4];
  966.     u_char    preview_duration[4];
  967.     u_char    poster_time[4];
  968.     u_char    selection_time[4];
  969.     u_char    selection_duration[4];
  970.     u_char    current_time[4];
  971.     u_char    next_track_id[4];
  972. } ngx_mp4_mvhd_atom_t;

  973. typedef struct {
  974.     u_char    size[4];
  975.     u_char    name[4];
  976.     u_char    version[1];
  977.     u_char    flags[3];
  978.     u_char    creation_time[8];
  979.     u_char    modification_time[8];
  980.     u_char    timescale[4];
  981.     u_char    duration[8];
  982.     u_char    rate[4];
  983.     u_char    volume[2];
  984.     u_char    reserved[10];
  985.     u_char    matrix[36];
  986.     u_char    preview_time[4];
  987.     u_char    preview_duration[4];
  988.     u_char    poster_time[4];
  989.     u_char    selection_time[4];
  990.     u_char    selection_duration[4];
  991.     u_char    current_time[4];
  992.     u_char    next_track_id[4];
  993. } ngx_mp4_mvhd64_atom_t;


  994. static ngx_int_t
  995. ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  996. {
  997.     u_char                 *atom_header;
  998.     size_t                  atom_size;
  999.     uint32_t                timescale;
  1000.     uint64_t                duration, start_time, length_time;
  1001.     ngx_buf_t              *atom;
  1002.     ngx_mp4_mvhd_atom_t    *mvhd_atom;
  1003.     ngx_mp4_mvhd64_atom_t  *mvhd64_atom;

  1004.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mvhd atom");

  1005.     atom_header = ngx_mp4_atom_header(mp4);
  1006.     mvhd_atom = (ngx_mp4_mvhd_atom_t *) atom_header;
  1007.     mvhd64_atom = (ngx_mp4_mvhd64_atom_t *) atom_header;
  1008.     ngx_mp4_set_atom_name(atom_header, 'm', 'v', 'h', 'd');

  1009.     if (ngx_mp4_atom_data_size(ngx_mp4_mvhd_atom_t) > atom_data_size) {
  1010.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1011.                       "\"%s\" mp4 mvhd atom too small", mp4->file.name.data);
  1012.         return NGX_ERROR;
  1013.     }

  1014.     if (mvhd_atom->version[0] == 0) {
  1015.         /* version 0: 32-bit duration */
  1016.         timescale = ngx_mp4_get_32value(mvhd_atom->timescale);
  1017.         duration = ngx_mp4_get_32value(mvhd_atom->duration);

  1018.     } else {
  1019.         /* version 1: 64-bit duration */

  1020.         if (ngx_mp4_atom_data_size(ngx_mp4_mvhd64_atom_t) > atom_data_size) {
  1021.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1022.                           "\"%s\" mp4 mvhd atom too small",
  1023.                           mp4->file.name.data);
  1024.             return NGX_ERROR;
  1025.         }

  1026.         timescale = ngx_mp4_get_32value(mvhd64_atom->timescale);
  1027.         duration = ngx_mp4_get_64value(mvhd64_atom->duration);
  1028.     }

  1029.     mp4->timescale = timescale;

  1030.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1031.                    "mvhd timescale:%uD, duration:%uL, time:%.3fs",
  1032.                    timescale, duration, (double) duration / timescale);

  1033.     start_time = (uint64_t) mp4->start * timescale / 1000;

  1034.     if (duration < start_time) {
  1035.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1036.                       "\"%s\" mp4 start time exceeds file duration",
  1037.                       mp4->file.name.data);
  1038.         return NGX_ERROR;
  1039.     }

  1040.     duration -= start_time;

  1041.     if (mp4->length) {
  1042.         length_time = (uint64_t) mp4->length * timescale / 1000;

  1043.         if (duration > length_time) {
  1044.             duration = length_time;
  1045.         }
  1046.     }

  1047.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1048.                    "mvhd new duration:%uL, time:%.3fs",
  1049.                    duration, (double) duration / timescale);

  1050.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1051.     ngx_mp4_set_32value(mvhd_atom->size, atom_size);

  1052.     if (mvhd_atom->version[0] == 0) {
  1053.         ngx_mp4_set_32value(mvhd_atom->duration, duration);

  1054.     } else {
  1055.         ngx_mp4_set_64value(mvhd64_atom->duration, duration);
  1056.     }

  1057.     atom = &mp4->mvhd_atom_buf;
  1058.     atom->temporary = 1;
  1059.     atom->pos = atom_header;
  1060.     atom->last = atom_header + atom_size;

  1061.     mp4->mvhd_atom.buf = atom;

  1062.     ngx_mp4_atom_next(mp4, atom_data_size);

  1063.     return NGX_OK;
  1064. }


  1065. static ngx_int_t
  1066. ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1067. {
  1068.     u_char               *atom_header, *atom_end;
  1069.     off_t                 atom_file_end;
  1070.     ngx_int_t             rc;
  1071.     ngx_buf_t            *atom;
  1072.     ngx_http_mp4_trak_t  *trak;

  1073.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 trak atom");

  1074.     trak = ngx_array_push(&mp4->trak);
  1075.     if (trak == NULL) {
  1076.         return NGX_ERROR;
  1077.     }

  1078.     ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t));

  1079.     atom_header = ngx_mp4_atom_header(mp4);
  1080.     ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k');

  1081.     atom = &trak->trak_atom_buf;
  1082.     atom->temporary = 1;
  1083.     atom->pos = atom_header;
  1084.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1085.     trak->out[NGX_HTTP_MP4_TRAK_ATOM].buf = atom;

  1086.     atom_end = mp4->buffer_pos + (size_t) atom_data_size;
  1087.     atom_file_end = mp4->offset + atom_data_size;

  1088.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_trak_atoms, atom_data_size);

  1089.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1090.                    "mp4 trak atom: %i", rc);

  1091.     if (rc == NGX_DECLINED) {
  1092.         /* skip this trak */
  1093.         ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t));
  1094.         mp4->trak.nelts--;
  1095.         mp4->buffer_pos = atom_end;
  1096.         mp4->offset = atom_file_end;
  1097.         return NGX_OK;
  1098.     }

  1099.     return rc;
  1100. }


  1101. static void
  1102. ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
  1103.     ngx_http_mp4_trak_t *trak)
  1104. {
  1105.     ngx_buf_t  *atom;

  1106.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1107.     atom = &trak->trak_atom_buf;
  1108.     ngx_mp4_set_32value(atom->pos, trak->size);
  1109. }


  1110. static ngx_int_t
  1111. ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1112. {
  1113.     ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1114.                   "\"%s\" mp4 compressed moov atom (cmov) is not supported",
  1115.                   mp4->file.name.data);

  1116.     return NGX_ERROR;
  1117. }


  1118. typedef struct {
  1119.     u_char    size[4];
  1120.     u_char    name[4];
  1121.     u_char    version[1];
  1122.     u_char    flags[3];
  1123.     u_char    creation_time[4];
  1124.     u_char    modification_time[4];
  1125.     u_char    track_id[4];
  1126.     u_char    reserved1[4];
  1127.     u_char    duration[4];
  1128.     u_char    reserved2[8];
  1129.     u_char    layer[2];
  1130.     u_char    group[2];
  1131.     u_char    volume[2];
  1132.     u_char    reverved3[2];
  1133.     u_char    matrix[36];
  1134.     u_char    width[4];
  1135.     u_char    heigth[4];
  1136. } ngx_mp4_tkhd_atom_t;

  1137. typedef struct {
  1138.     u_char    size[4];
  1139.     u_char    name[4];
  1140.     u_char    version[1];
  1141.     u_char    flags[3];
  1142.     u_char    creation_time[8];
  1143.     u_char    modification_time[8];
  1144.     u_char    track_id[4];
  1145.     u_char    reserved1[4];
  1146.     u_char    duration[8];
  1147.     u_char    reserved2[8];
  1148.     u_char    layer[2];
  1149.     u_char    group[2];
  1150.     u_char    volume[2];
  1151.     u_char    reverved3[2];
  1152.     u_char    matrix[36];
  1153.     u_char    width[4];
  1154.     u_char    heigth[4];
  1155. } ngx_mp4_tkhd64_atom_t;


  1156. static ngx_int_t
  1157. ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1158. {
  1159.     u_char                 *atom_header;
  1160.     size_t                  atom_size;
  1161.     uint64_t                duration, start_time, length_time;
  1162.     ngx_buf_t              *atom;
  1163.     ngx_http_mp4_trak_t    *trak;
  1164.     ngx_mp4_tkhd_atom_t    *tkhd_atom;
  1165.     ngx_mp4_tkhd64_atom_t  *tkhd64_atom;

  1166.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 tkhd atom");

  1167.     atom_header = ngx_mp4_atom_header(mp4);
  1168.     tkhd_atom = (ngx_mp4_tkhd_atom_t *) atom_header;
  1169.     tkhd64_atom = (ngx_mp4_tkhd64_atom_t *) atom_header;
  1170.     ngx_mp4_set_atom_name(tkhd_atom, 't', 'k', 'h', 'd');

  1171.     if (ngx_mp4_atom_data_size(ngx_mp4_tkhd_atom_t) > atom_data_size) {
  1172.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1173.                       "\"%s\" mp4 tkhd atom too small", mp4->file.name.data);
  1174.         return NGX_ERROR;
  1175.     }

  1176.     if (tkhd_atom->version[0] == 0) {
  1177.         /* version 0: 32-bit duration */
  1178.         duration = ngx_mp4_get_32value(tkhd_atom->duration);

  1179.     } else {
  1180.         /* version 1: 64-bit duration */

  1181.         if (ngx_mp4_atom_data_size(ngx_mp4_tkhd64_atom_t) > atom_data_size) {
  1182.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1183.                           "\"%s\" mp4 tkhd atom too small",
  1184.                           mp4->file.name.data);
  1185.             return NGX_ERROR;
  1186.         }

  1187.         duration = ngx_mp4_get_64value(tkhd64_atom->duration);
  1188.     }

  1189.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1190.                    "tkhd duration:%uL, time:%.3fs",
  1191.                    duration, (double) duration / mp4->timescale);

  1192.     start_time = (uint64_t) mp4->start * mp4->timescale / 1000;

  1193.     if (duration <= start_time) {
  1194.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1195.                        "tkhd duration is less than start time");
  1196.         return NGX_DECLINED;
  1197.     }

  1198.     duration -= start_time;

  1199.     if (mp4->length) {
  1200.         length_time = (uint64_t) mp4->length * mp4->timescale / 1000;

  1201.         if (duration > length_time) {
  1202.             duration = length_time;
  1203.         }
  1204.     }

  1205.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1206.                    "tkhd new duration:%uL, time:%.3fs",
  1207.                    duration, (double) duration / mp4->timescale);

  1208.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  1209.     trak = ngx_mp4_last_trak(mp4);
  1210.     trak->tkhd_size = atom_size;

  1211.     ngx_mp4_set_32value(tkhd_atom->size, atom_size);

  1212.     if (tkhd_atom->version[0] == 0) {
  1213.         ngx_mp4_set_32value(tkhd_atom->duration, duration);

  1214.     } else {
  1215.         ngx_mp4_set_64value(tkhd64_atom->duration, duration);
  1216.     }

  1217.     atom = &trak->tkhd_atom_buf;
  1218.     atom->temporary = 1;
  1219.     atom->pos = atom_header;
  1220.     atom->last = atom_header + atom_size;

  1221.     trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf = atom;

  1222.     ngx_mp4_atom_next(mp4, atom_data_size);

  1223.     return NGX_OK;
  1224. }


  1225. static ngx_int_t
  1226. ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1227. {
  1228.     u_char               *atom_header;
  1229.     ngx_buf_t            *atom;
  1230.     ngx_http_mp4_trak_t  *trak;

  1231.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process mdia atom");

  1232.     atom_header = ngx_mp4_atom_header(mp4);
  1233.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a');

  1234.     trak = ngx_mp4_last_trak(mp4);

  1235.     atom = &trak->mdia_atom_buf;
  1236.     atom->temporary = 1;
  1237.     atom->pos = atom_header;
  1238.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1239.     trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf = atom;

  1240.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size);
  1241. }


  1242. static void
  1243. ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
  1244.     ngx_http_mp4_trak_t *trak)
  1245. {
  1246.     ngx_buf_t  *atom;

  1247.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1248.     atom = &trak->mdia_atom_buf;
  1249.     ngx_mp4_set_32value(atom->pos, trak->size);
  1250. }


  1251. typedef struct {
  1252.     u_char    size[4];
  1253.     u_char    name[4];
  1254.     u_char    version[1];
  1255.     u_char    flags[3];
  1256.     u_char    creation_time[4];
  1257.     u_char    modification_time[4];
  1258.     u_char    timescale[4];
  1259.     u_char    duration[4];
  1260.     u_char    language[2];
  1261.     u_char    quality[2];
  1262. } ngx_mp4_mdhd_atom_t;

  1263. typedef struct {
  1264.     u_char    size[4];
  1265.     u_char    name[4];
  1266.     u_char    version[1];
  1267.     u_char    flags[3];
  1268.     u_char    creation_time[8];
  1269.     u_char    modification_time[8];
  1270.     u_char    timescale[4];
  1271.     u_char    duration[8];
  1272.     u_char    language[2];
  1273.     u_char    quality[2];
  1274. } ngx_mp4_mdhd64_atom_t;


  1275. static ngx_int_t
  1276. ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1277. {
  1278.     u_char                 *atom_header;
  1279.     size_t                  atom_size;
  1280.     uint32_t                timescale;
  1281.     uint64_t                duration, start_time, length_time;
  1282.     ngx_buf_t              *atom;
  1283.     ngx_http_mp4_trak_t    *trak;
  1284.     ngx_mp4_mdhd_atom_t    *mdhd_atom;
  1285.     ngx_mp4_mdhd64_atom_t  *mdhd64_atom;

  1286.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdhd atom");

  1287.     atom_header = ngx_mp4_atom_header(mp4);
  1288.     mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom_header;
  1289.     mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom_header;
  1290.     ngx_mp4_set_atom_name(mdhd_atom, 'm', 'd', 'h', 'd');

  1291.     if (ngx_mp4_atom_data_size(ngx_mp4_mdhd_atom_t) > atom_data_size) {
  1292.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1293.                       "\"%s\" mp4 mdhd atom too small", mp4->file.name.data);
  1294.         return NGX_ERROR;
  1295.     }

  1296.     if (mdhd_atom->version[0] == 0) {
  1297.         /* version 0: everything is 32-bit */
  1298.         timescale = ngx_mp4_get_32value(mdhd_atom->timescale);
  1299.         duration = ngx_mp4_get_32value(mdhd_atom->duration);

  1300.     } else {
  1301.         /* version 1: 64-bit duration and 32-bit timescale */

  1302.         if (ngx_mp4_atom_data_size(ngx_mp4_mdhd64_atom_t) > atom_data_size) {
  1303.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1304.                           "\"%s\" mp4 mdhd atom too small",
  1305.                           mp4->file.name.data);
  1306.             return NGX_ERROR;
  1307.         }

  1308.         timescale = ngx_mp4_get_32value(mdhd64_atom->timescale);
  1309.         duration = ngx_mp4_get_64value(mdhd64_atom->duration);
  1310.     }

  1311.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1312.                    "mdhd timescale:%uD, duration:%uL, time:%.3fs",
  1313.                    timescale, duration, (double) duration / timescale);

  1314.     start_time = (uint64_t) mp4->start * timescale / 1000;

  1315.     if (duration <= start_time) {
  1316.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1317.                        "mdhd duration is less than start time");
  1318.         return NGX_DECLINED;
  1319.     }

  1320.     duration -= start_time;

  1321.     if (mp4->length) {
  1322.         length_time = (uint64_t) mp4->length * timescale / 1000;

  1323.         if (duration > length_time) {
  1324.             duration = length_time;
  1325.         }
  1326.     }

  1327.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1328.                    "mdhd new duration:%uL, time:%.3fs",
  1329.                    duration, (double) duration / timescale);

  1330.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  1331.     trak = ngx_mp4_last_trak(mp4);
  1332.     trak->mdhd_size = atom_size;
  1333.     trak->timescale = timescale;

  1334.     ngx_mp4_set_32value(mdhd_atom->size, atom_size);

  1335.     if (mdhd_atom->version[0] == 0) {
  1336.         ngx_mp4_set_32value(mdhd_atom->duration, duration);

  1337.     } else {
  1338.         ngx_mp4_set_64value(mdhd64_atom->duration, duration);
  1339.     }

  1340.     atom = &trak->mdhd_atom_buf;
  1341.     atom->temporary = 1;
  1342.     atom->pos = atom_header;
  1343.     atom->last = atom_header + atom_size;

  1344.     trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom;

  1345.     ngx_mp4_atom_next(mp4, atom_data_size);

  1346.     return NGX_OK;
  1347. }


  1348. static ngx_int_t
  1349. ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1350. {
  1351.     u_char              *atom_header;
  1352.     size_t               atom_size;
  1353.     ngx_buf_t            *atom;
  1354.     ngx_http_mp4_trak_t  *trak;

  1355.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 hdlr atom");

  1356.     atom_header = ngx_mp4_atom_header(mp4);
  1357.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1358.     ngx_mp4_set_32value(atom_header, atom_size);
  1359.     ngx_mp4_set_atom_name(atom_header, 'h', 'd', 'l', 'r');

  1360.     trak = ngx_mp4_last_trak(mp4);

  1361.     atom = &trak->hdlr_atom_buf;
  1362.     atom->temporary = 1;
  1363.     atom->pos = atom_header;
  1364.     atom->last = atom_header + atom_size;

  1365.     trak->hdlr_size = atom_size;
  1366.     trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom;

  1367.     ngx_mp4_atom_next(mp4, atom_data_size);

  1368.     return NGX_OK;
  1369. }


  1370. static ngx_int_t
  1371. ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1372. {
  1373.     u_char               *atom_header;
  1374.     ngx_buf_t            *atom;
  1375.     ngx_http_mp4_trak_t  *trak;

  1376.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process minf atom");

  1377.     atom_header = ngx_mp4_atom_header(mp4);
  1378.     ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f');

  1379.     trak = ngx_mp4_last_trak(mp4);

  1380.     atom = &trak->minf_atom_buf;
  1381.     atom->temporary = 1;
  1382.     atom->pos = atom_header;
  1383.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1384.     trak->out[NGX_HTTP_MP4_MINF_ATOM].buf = atom;

  1385.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size);
  1386. }


  1387. static void
  1388. ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
  1389.     ngx_http_mp4_trak_t *trak)
  1390. {
  1391.     ngx_buf_t  *atom;

  1392.     trak->size += sizeof(ngx_mp4_atom_header_t)
  1393.                + trak->vmhd_size
  1394.                + trak->smhd_size
  1395.                + trak->dinf_size;
  1396.     atom = &trak->minf_atom_buf;
  1397.     ngx_mp4_set_32value(atom->pos, trak->size);
  1398. }


  1399. static ngx_int_t
  1400. ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1401. {
  1402.     u_char              *atom_header;
  1403.     size_t               atom_size;
  1404.     ngx_buf_t            *atom;
  1405.     ngx_http_mp4_trak_t  *trak;

  1406.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 vmhd atom");

  1407.     atom_header = ngx_mp4_atom_header(mp4);
  1408.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1409.     ngx_mp4_set_32value(atom_header, atom_size);
  1410.     ngx_mp4_set_atom_name(atom_header, 'v', 'm', 'h', 'd');

  1411.     trak = ngx_mp4_last_trak(mp4);

  1412.     atom = &trak->vmhd_atom_buf;
  1413.     atom->temporary = 1;
  1414.     atom->pos = atom_header;
  1415.     atom->last = atom_header + atom_size;

  1416.     trak->vmhd_size += atom_size;
  1417.     trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom;

  1418.     ngx_mp4_atom_next(mp4, atom_data_size);

  1419.     return NGX_OK;
  1420. }


  1421. static ngx_int_t
  1422. ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1423. {
  1424.     u_char              *atom_header;
  1425.     size_t               atom_size;
  1426.     ngx_buf_t            *atom;
  1427.     ngx_http_mp4_trak_t  *trak;

  1428.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 smhd atom");

  1429.     atom_header = ngx_mp4_atom_header(mp4);
  1430.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1431.     ngx_mp4_set_32value(atom_header, atom_size);
  1432.     ngx_mp4_set_atom_name(atom_header, 's', 'm', 'h', 'd');

  1433.     trak = ngx_mp4_last_trak(mp4);

  1434.     atom = &trak->smhd_atom_buf;
  1435.     atom->temporary = 1;
  1436.     atom->pos = atom_header;
  1437.     atom->last = atom_header + atom_size;

  1438.     trak->smhd_size += atom_size;
  1439.     trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom;

  1440.     ngx_mp4_atom_next(mp4, atom_data_size);

  1441.     return NGX_OK;
  1442. }


  1443. static ngx_int_t
  1444. ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1445. {
  1446.     u_char              *atom_header;
  1447.     size_t               atom_size;
  1448.     ngx_buf_t            *atom;
  1449.     ngx_http_mp4_trak_t  *trak;

  1450.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 dinf atom");

  1451.     atom_header = ngx_mp4_atom_header(mp4);
  1452.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1453.     ngx_mp4_set_32value(atom_header, atom_size);
  1454.     ngx_mp4_set_atom_name(atom_header, 'd', 'i', 'n', 'f');

  1455.     trak = ngx_mp4_last_trak(mp4);

  1456.     atom = &trak->dinf_atom_buf;
  1457.     atom->temporary = 1;
  1458.     atom->pos = atom_header;
  1459.     atom->last = atom_header + atom_size;

  1460.     trak->dinf_size += atom_size;
  1461.     trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom;

  1462.     ngx_mp4_atom_next(mp4, atom_data_size);

  1463.     return NGX_OK;
  1464. }


  1465. static ngx_int_t
  1466. ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1467. {
  1468.     u_char               *atom_header;
  1469.     ngx_buf_t            *atom;
  1470.     ngx_http_mp4_trak_t  *trak;

  1471.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process stbl atom");

  1472.     atom_header = ngx_mp4_atom_header(mp4);
  1473.     ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l');

  1474.     trak = ngx_mp4_last_trak(mp4);

  1475.     atom = &trak->stbl_atom_buf;
  1476.     atom->temporary = 1;
  1477.     atom->pos = atom_header;
  1478.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1479.     trak->out[NGX_HTTP_MP4_STBL_ATOM].buf = atom;

  1480.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size);
  1481. }


  1482. static void
  1483. ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
  1484.     ngx_http_mp4_trak_t *trak)
  1485. {
  1486.     ngx_buf_t  *atom;

  1487.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1488.     atom = &trak->stbl_atom_buf;
  1489.     ngx_mp4_set_32value(atom->pos, trak->size);
  1490. }


  1491. typedef struct {
  1492.     u_char    size[4];
  1493.     u_char    name[4];
  1494.     u_char    version[1];
  1495.     u_char    flags[3];
  1496.     u_char    entries[4];

  1497.     u_char    media_size[4];
  1498.     u_char    media_name[4];
  1499. } ngx_mp4_stsd_atom_t;


  1500. static ngx_int_t
  1501. ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1502. {
  1503.     u_char               *atom_header, *atom_table;
  1504.     size_t                atom_size;
  1505.     ngx_buf_t            *atom;
  1506.     ngx_mp4_stsd_atom_t  *stsd_atom;
  1507.     ngx_http_mp4_trak_t  *trak;

  1508.     /* sample description atom */

  1509.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsd atom");

  1510.     atom_header = ngx_mp4_atom_header(mp4);
  1511.     stsd_atom = (ngx_mp4_stsd_atom_t *) atom_header;
  1512.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1513.     atom_table = atom_header + atom_size;
  1514.     ngx_mp4_set_32value(stsd_atom->size, atom_size);
  1515.     ngx_mp4_set_atom_name(stsd_atom, 's', 't', 's', 'd');

  1516.     if (ngx_mp4_atom_data_size(ngx_mp4_stsd_atom_t) > atom_data_size) {
  1517.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1518.                       "\"%s\" mp4 stsd atom too small", mp4->file.name.data);
  1519.         return NGX_ERROR;
  1520.     }

  1521.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1522.                    "stsd entries:%uD, media:%*s",
  1523.                    ngx_mp4_get_32value(stsd_atom->entries),
  1524.                    4, stsd_atom->media_name);

  1525.     trak = ngx_mp4_last_trak(mp4);

  1526.     atom = &trak->stsd_atom_buf;
  1527.     atom->temporary = 1;
  1528.     atom->pos = atom_header;
  1529.     atom->last = atom_table;

  1530.     trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom;
  1531.     trak->size += atom_size;

  1532.     ngx_mp4_atom_next(mp4, atom_data_size);

  1533.     return NGX_OK;
  1534. }


  1535. typedef struct {
  1536.     u_char    size[4];
  1537.     u_char    name[4];
  1538.     u_char    version[1];
  1539.     u_char    flags[3];
  1540.     u_char    entries[4];
  1541. } ngx_mp4_stts_atom_t;

  1542. typedef struct {
  1543.     u_char    count[4];
  1544.     u_char    duration[4];
  1545. } ngx_mp4_stts_entry_t;


  1546. static ngx_int_t
  1547. ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1548. {
  1549.     u_char               *atom_header, *atom_table, *atom_end;
  1550.     uint32_t              entries;
  1551.     ngx_buf_t            *atom, *data;
  1552.     ngx_mp4_stts_atom_t  *stts_atom;
  1553.     ngx_http_mp4_trak_t  *trak;

  1554.     /* time-to-sample atom */

  1555.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stts atom");

  1556.     atom_header = ngx_mp4_atom_header(mp4);
  1557.     stts_atom = (ngx_mp4_stts_atom_t *) atom_header;
  1558.     ngx_mp4_set_atom_name(stts_atom, 's', 't', 't', 's');

  1559.     if (ngx_mp4_atom_data_size(ngx_mp4_stts_atom_t) > atom_data_size) {
  1560.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1561.                       "\"%s\" mp4 stts atom too small", mp4->file.name.data);
  1562.         return NGX_ERROR;
  1563.     }

  1564.     entries = ngx_mp4_get_32value(stts_atom->entries);

  1565.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1566.                    "mp4 time-to-sample entries:%uD", entries);

  1567.     if (ngx_mp4_atom_data_size(ngx_mp4_stts_atom_t)
  1568.         + entries * sizeof(ngx_mp4_stts_entry_t) > atom_data_size)
  1569.     {
  1570.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1571.                       "\"%s\" mp4 stts atom too small", mp4->file.name.data);
  1572.         return NGX_ERROR;
  1573.     }

  1574.     atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t);
  1575.     atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t);

  1576.     trak = ngx_mp4_last_trak(mp4);
  1577.     trak->time_to_sample_entries = entries;

  1578.     atom = &trak->stts_atom_buf;
  1579.     atom->temporary = 1;
  1580.     atom->pos = atom_header;
  1581.     atom->last = atom_table;

  1582.     data = &trak->stts_data_buf;
  1583.     data->temporary = 1;
  1584.     data->pos = atom_table;
  1585.     data->last = atom_end;

  1586.     trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom;
  1587.     trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data;

  1588.     ngx_mp4_atom_next(mp4, atom_data_size);

  1589.     return NGX_OK;
  1590. }


  1591. static ngx_int_t
  1592. ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
  1593.     ngx_http_mp4_trak_t *trak)
  1594. {
  1595.     size_t                atom_size;
  1596.     ngx_buf_t            *atom, *data;
  1597.     ngx_mp4_stts_atom_t  *stts_atom;

  1598.     /*
  1599.      * mdia.minf.stbl.stts updating requires trak->timescale
  1600.      * from mdia.mdhd atom which may reside after mdia.minf
  1601.      */

  1602.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1603.                    "mp4 stts atom update");

  1604.     data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;

  1605.     if (data == NULL) {
  1606.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1607.                       "no mp4 stts atoms were found in \"%s\"",
  1608.                       mp4->file.name.data);
  1609.         return NGX_ERROR;
  1610.     }

  1611.     if (ngx_http_mp4_crop_stts_data(mp4, trak, 1) != NGX_OK) {
  1612.         return NGX_ERROR;
  1613.     }

  1614.     if (ngx_http_mp4_crop_stts_data(mp4, trak, 0) != NGX_OK) {
  1615.         return NGX_ERROR;
  1616.     }

  1617.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1618.                    "time-to-sample entries:%uD", trak->time_to_sample_entries);

  1619.     atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
  1620.     trak->size += atom_size;

  1621.     atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
  1622.     stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
  1623.     ngx_mp4_set_32value(stts_atom->size, atom_size);
  1624.     ngx_mp4_set_32value(stts_atom->entries, trak->time_to_sample_entries);

  1625.     return NGX_OK;
  1626. }


  1627. static ngx_int_t
  1628. ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
  1629.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  1630. {
  1631.     uint32_t               count, duration, rest;
  1632.     uint64_t               start_time;
  1633.     ngx_buf_t             *data;
  1634.     ngx_uint_t             start_sample, entries, start_sec;
  1635.     ngx_mp4_stts_entry_t  *entry, *end;

  1636.     if (start) {
  1637.         start_sec = mp4->start;

  1638.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1639.                        "mp4 stts crop start_time:%ui", start_sec);

  1640.     } else if (mp4->length) {
  1641.         start_sec = mp4->length;

  1642.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1643.                        "mp4 stts crop end_time:%ui", start_sec);

  1644.     } else {
  1645.         return NGX_OK;
  1646.     }

  1647.     data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;

  1648.     start_time = (uint64_t) start_sec * trak->timescale / 1000;

  1649.     entries = trak->time_to_sample_entries;
  1650.     start_sample = 0;
  1651.     entry = (ngx_mp4_stts_entry_t *) data->pos;
  1652.     end = (ngx_mp4_stts_entry_t *) data->last;

  1653.     while (entry < end) {
  1654.         count = ngx_mp4_get_32value(entry->count);
  1655.         duration = ngx_mp4_get_32value(entry->duration);

  1656.         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1657.                        "time:%uL, count:%uD, duration:%uD",
  1658.                        start_time, count, duration);

  1659.         if (start_time < (uint64_t) count * duration) {
  1660.             start_sample += (ngx_uint_t) (start_time / duration);
  1661.             rest = (uint32_t) (start_time / duration);
  1662.             goto found;
  1663.         }

  1664.         start_sample += count;
  1665.         start_time -= count * duration;
  1666.         entries--;
  1667.         entry++;
  1668.     }

  1669.     if (start) {
  1670.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1671.                       "start time is out mp4 stts samples in \"%s\"",
  1672.                       mp4->file.name.data);

  1673.         return NGX_ERROR;

  1674.     } else {
  1675.         trak->end_sample = trak->start_sample + start_sample;

  1676.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1677.                        "end_sample:%ui", trak->end_sample);

  1678.         return NGX_OK;
  1679.     }

  1680. found:

  1681.     if (start) {
  1682.         ngx_mp4_set_32value(entry->count, count - rest);
  1683.         data->pos = (u_char *) entry;
  1684.         trak->time_to_sample_entries = entries;
  1685.         trak->start_sample = start_sample;

  1686.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1687.                        "start_sample:%ui, new count:%uD",
  1688.                        trak->start_sample, count - rest);

  1689.     } else {
  1690.         ngx_mp4_set_32value(entry->count, rest);
  1691.         data->last = (u_char *) (entry + 1);
  1692.         trak->time_to_sample_entries -= entries - 1;
  1693.         trak->end_sample = trak->start_sample + start_sample;

  1694.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1695.                        "end_sample:%ui, new count:%uD",
  1696.                        trak->end_sample, rest);
  1697.     }

  1698.     return NGX_OK;
  1699. }


  1700. typedef struct {
  1701.     u_char    size[4];
  1702.     u_char    name[4];
  1703.     u_char    version[1];
  1704.     u_char    flags[3];
  1705.     u_char    entries[4];
  1706. } ngx_http_mp4_stss_atom_t;


  1707. static ngx_int_t
  1708. ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1709. {
  1710.     u_char                    *atom_header, *atom_table, *atom_end;
  1711.     uint32_t                   entries;
  1712.     ngx_buf_t                 *atom, *data;
  1713.     ngx_http_mp4_trak_t       *trak;
  1714.     ngx_http_mp4_stss_atom_t  *stss_atom;

  1715.     /* sync samples atom */

  1716.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stss atom");

  1717.     atom_header = ngx_mp4_atom_header(mp4);
  1718.     stss_atom = (ngx_http_mp4_stss_atom_t *) atom_header;
  1719.     ngx_mp4_set_atom_name(stss_atom, 's', 't', 's', 's');

  1720.     if (ngx_mp4_atom_data_size(ngx_http_mp4_stss_atom_t) > atom_data_size) {
  1721.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1722.                       "\"%s\" mp4 stss atom too small", mp4->file.name.data);
  1723.         return NGX_ERROR;
  1724.     }

  1725.     entries = ngx_mp4_get_32value(stss_atom->entries);

  1726.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1727.                    "sync sample entries:%uD", entries);

  1728.     trak = ngx_mp4_last_trak(mp4);
  1729.     trak->sync_samples_entries = entries;

  1730.     atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t);

  1731.     atom = &trak->stss_atom_buf;
  1732.     atom->temporary = 1;
  1733.     atom->pos = atom_header;
  1734.     atom->last = atom_table;

  1735.     if (ngx_mp4_atom_data_size(ngx_http_mp4_stss_atom_t)
  1736.         + entries * sizeof(uint32_t) > atom_data_size)
  1737.     {
  1738.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1739.                       "\"%s\" mp4 stss atom too small", mp4->file.name.data);
  1740.         return NGX_ERROR;
  1741.     }

  1742.     atom_end = atom_table + entries * sizeof(uint32_t);

  1743.     data = &trak->stss_data_buf;
  1744.     data->temporary = 1;
  1745.     data->pos = atom_table;
  1746.     data->last = atom_end;

  1747.     trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom;
  1748.     trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data;

  1749.     ngx_mp4_atom_next(mp4, atom_data_size);

  1750.     return NGX_OK;
  1751. }


  1752. static ngx_int_t
  1753. ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
  1754.     ngx_http_mp4_trak_t *trak)
  1755. {
  1756.     size_t                     atom_size;
  1757.     uint32_t                   sample, start_sample, *entry, *end;
  1758.     ngx_buf_t                 *atom, *data;
  1759.     ngx_http_mp4_stss_atom_t  *stss_atom;

  1760.     /*
  1761.      * mdia.minf.stbl.stss updating requires trak->start_sample
  1762.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  1763.      * atom which may reside after mdia.minf
  1764.      */

  1765.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1766.                    "mp4 stss atom update");

  1767.     data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;

  1768.     if (data == NULL) {
  1769.         return NGX_OK;
  1770.     }

  1771.     ngx_http_mp4_crop_stss_data(mp4, trak, 1);
  1772.     ngx_http_mp4_crop_stss_data(mp4, trak, 0);

  1773.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1774.                    "sync sample entries:%uD", trak->sync_samples_entries);

  1775.     if (trak->sync_samples_entries) {
  1776.         entry = (uint32_t *) data->pos;
  1777.         end = (uint32_t *) data->last;

  1778.         start_sample = trak->start_sample;

  1779.         while (entry < end) {
  1780.             sample = ngx_mp4_get_32value(entry);
  1781.             sample -= start_sample;
  1782.             ngx_mp4_set_32value(entry, sample);
  1783.             entry++;
  1784.         }

  1785.     } else {
  1786.         trak->out[NGX_HTTP_MP4_STSS_DATA].buf = NULL;
  1787.     }

  1788.     atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos);
  1789.     trak->size += atom_size;

  1790.     atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf;
  1791.     stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos;

  1792.     ngx_mp4_set_32value(stss_atom->size, atom_size);
  1793.     ngx_mp4_set_32value(stss_atom->entries, trak->sync_samples_entries);

  1794.     return NGX_OK;
  1795. }


  1796. static void
  1797. ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
  1798.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  1799. {
  1800.     uint32_t     sample, start_sample, *entry, *end;
  1801.     ngx_buf_t   *data;
  1802.     ngx_uint_t   entries;

  1803.     /* sync samples starts from 1 */

  1804.     if (start) {
  1805.         start_sample = trak->start_sample + 1;

  1806.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1807.                        "mp4 stss crop start_sample:%uD", start_sample);

  1808.     } else if (mp4->length) {
  1809.         start_sample = trak->end_sample + 1;

  1810.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1811.                        "mp4 stss crop end_sample:%uD", start_sample);

  1812.     } else {
  1813.         return;
  1814.     }

  1815.     data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;

  1816.     entries = trak->sync_samples_entries;
  1817.     entry = (uint32_t *) data->pos;
  1818.     end = (uint32_t *) data->last;

  1819.     while (entry < end) {
  1820.         sample = ngx_mp4_get_32value(entry);

  1821.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1822.                        "sync:%uD", sample);

  1823.         if (sample >= start_sample) {
  1824.             goto found;
  1825.         }

  1826.         entries--;
  1827.         entry++;
  1828.     }

  1829.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1830.                    "sample is out of mp4 stss atom");

  1831. found:

  1832.     if (start) {
  1833.         data->pos = (u_char *) entry;
  1834.         trak->sync_samples_entries = entries;

  1835.     } else {
  1836.         data->last = (u_char *) entry;
  1837.         trak->sync_samples_entries -= entries;
  1838.     }
  1839. }


  1840. typedef struct {
  1841.     u_char    size[4];
  1842.     u_char    name[4];
  1843.     u_char    version[1];
  1844.     u_char    flags[3];
  1845.     u_char    entries[4];
  1846. } ngx_mp4_ctts_atom_t;

  1847. typedef struct {
  1848.     u_char    count[4];
  1849.     u_char    offset[4];
  1850. } ngx_mp4_ctts_entry_t;


  1851. static ngx_int_t
  1852. ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1853. {
  1854.     u_char               *atom_header, *atom_table, *atom_end;
  1855.     uint32_t              entries;
  1856.     ngx_buf_t            *atom, *data;
  1857.     ngx_mp4_ctts_atom_t  *ctts_atom;
  1858.     ngx_http_mp4_trak_t  *trak;

  1859.     /* composition offsets atom */

  1860.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ctts atom");

  1861.     atom_header = ngx_mp4_atom_header(mp4);
  1862.     ctts_atom = (ngx_mp4_ctts_atom_t *) atom_header;
  1863.     ngx_mp4_set_atom_name(ctts_atom, 'c', 't', 't', 's');

  1864.     if (ngx_mp4_atom_data_size(ngx_mp4_ctts_atom_t) > atom_data_size) {
  1865.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1866.                       "\"%s\" mp4 ctts atom too small", mp4->file.name.data);
  1867.         return NGX_ERROR;
  1868.     }

  1869.     entries = ngx_mp4_get_32value(ctts_atom->entries);

  1870.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1871.                    "composition offset entries:%uD", entries);

  1872.     trak = ngx_mp4_last_trak(mp4);
  1873.     trak->composition_offset_entries = entries;

  1874.     atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t);

  1875.     atom = &trak->ctts_atom_buf;
  1876.     atom->temporary = 1;
  1877.     atom->pos = atom_header;
  1878.     atom->last = atom_table;

  1879.     if (ngx_mp4_atom_data_size(ngx_mp4_ctts_atom_t)
  1880.         + entries * sizeof(ngx_mp4_ctts_entry_t) > atom_data_size)
  1881.     {
  1882.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1883.                       "\"%s\" mp4 ctts atom too small", mp4->file.name.data);
  1884.         return NGX_ERROR;
  1885.     }

  1886.     atom_end = atom_table + entries * sizeof(ngx_mp4_ctts_entry_t);

  1887.     data = &trak->ctts_data_buf;
  1888.     data->temporary = 1;
  1889.     data->pos = atom_table;
  1890.     data->last = atom_end;

  1891.     trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom;
  1892.     trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data;

  1893.     ngx_mp4_atom_next(mp4, atom_data_size);

  1894.     return NGX_OK;
  1895. }


  1896. static void
  1897. ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
  1898.     ngx_http_mp4_trak_t *trak)
  1899. {
  1900.     size_t                atom_size;
  1901.     ngx_buf_t            *atom, *data;
  1902.     ngx_mp4_ctts_atom_t  *ctts_atom;

  1903.     /*
  1904.      * mdia.minf.stbl.ctts updating requires trak->start_sample
  1905.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  1906.      * atom which may reside after mdia.minf
  1907.      */

  1908.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1909.                    "mp4 ctts atom update");

  1910.     data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;

  1911.     if (data == NULL) {
  1912.         return;
  1913.     }

  1914.     ngx_http_mp4_crop_ctts_data(mp4, trak, 1);
  1915.     ngx_http_mp4_crop_ctts_data(mp4, trak, 0);

  1916.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1917.                    "composition offset entries:%uD",
  1918.                    trak->composition_offset_entries);

  1919.     if (trak->composition_offset_entries == 0) {
  1920.         trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
  1921.         trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
  1922.         return;
  1923.     }

  1924.     atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos);
  1925.     trak->size += atom_size;

  1926.     atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf;
  1927.     ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos;

  1928.     ngx_mp4_set_32value(ctts_atom->size, atom_size);
  1929.     ngx_mp4_set_32value(ctts_atom->entries, trak->composition_offset_entries);

  1930.     return;
  1931. }


  1932. static void
  1933. ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
  1934.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  1935. {
  1936.     uint32_t               count, start_sample, rest;
  1937.     ngx_buf_t             *data;
  1938.     ngx_uint_t             entries;
  1939.     ngx_mp4_ctts_entry_t  *entry, *end;

  1940.     /* sync samples starts from 1 */

  1941.     if (start) {
  1942.         start_sample = trak->start_sample + 1;

  1943.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1944.                        "mp4 ctts crop start_sample:%uD", start_sample);

  1945.     } else if (mp4->length) {
  1946.         start_sample = trak->end_sample - trak->start_sample + 1;

  1947.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1948.                        "mp4 ctts crop end_sample:%uD", start_sample);

  1949.     } else {
  1950.         return;
  1951.     }

  1952.     data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;

  1953.     entries = trak->composition_offset_entries;
  1954.     entry = (ngx_mp4_ctts_entry_t *) data->pos;
  1955.     end = (ngx_mp4_ctts_entry_t *) data->last;

  1956.     while (entry < end) {
  1957.         count = ngx_mp4_get_32value(entry->count);

  1958.         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1959.                        "sample:%uD, count:%uD, offset:%uD",
  1960.                        start_sample, count, ngx_mp4_get_32value(entry->offset));

  1961.          if (start_sample <= count) {
  1962.              rest = start_sample - 1;
  1963.              goto found;
  1964.          }

  1965.          start_sample -= count;
  1966.          entries--;
  1967.          entry++;
  1968.     }

  1969.     if (start) {
  1970.         data->pos = (u_char *) end;
  1971.         trak->composition_offset_entries = 0;
  1972.     }

  1973.     return;

  1974. found:

  1975.     if (start) {
  1976.         ngx_mp4_set_32value(entry->count, count - rest);
  1977.         data->pos = (u_char *) entry;
  1978.         trak->composition_offset_entries = entries;

  1979.     } else {
  1980.         ngx_mp4_set_32value(entry->count, rest);
  1981.         data->last = (u_char *) (entry + 1);
  1982.         trak->composition_offset_entries -= entries - 1;
  1983.     }
  1984. }


  1985. typedef struct {
  1986.     u_char    size[4];
  1987.     u_char    name[4];
  1988.     u_char    version[1];
  1989.     u_char    flags[3];
  1990.     u_char    entries[4];
  1991. } ngx_mp4_stsc_atom_t;


  1992. static ngx_int_t
  1993. ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1994. {
  1995.     u_char               *atom_header, *atom_table, *atom_end;
  1996.     uint32_t              entries;
  1997.     ngx_buf_t            *atom, *data;
  1998.     ngx_mp4_stsc_atom_t  *stsc_atom;
  1999.     ngx_http_mp4_trak_t  *trak;

  2000.     /* sample-to-chunk atom */

  2001.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsc atom");

  2002.     atom_header = ngx_mp4_atom_header(mp4);
  2003.     stsc_atom = (ngx_mp4_stsc_atom_t *) atom_header;
  2004.     ngx_mp4_set_atom_name(stsc_atom, 's', 't', 's', 'c');

  2005.     if (ngx_mp4_atom_data_size(ngx_mp4_stsc_atom_t) > atom_data_size) {
  2006.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2007.                       "\"%s\" mp4 stsc atom too small", mp4->file.name.data);
  2008.         return NGX_ERROR;
  2009.     }

  2010.     entries = ngx_mp4_get_32value(stsc_atom->entries);

  2011.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2012.                    "sample-to-chunk entries:%uD", entries);

  2013.     if (ngx_mp4_atom_data_size(ngx_mp4_stsc_atom_t)
  2014.         + entries * sizeof(ngx_mp4_stsc_entry_t) > atom_data_size)
  2015.     {
  2016.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2017.                       "\"%s\" mp4 stsc atom too small", mp4->file.name.data);
  2018.         return NGX_ERROR;
  2019.     }

  2020.     atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t);
  2021.     atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t);

  2022.     trak = ngx_mp4_last_trak(mp4);
  2023.     trak->sample_to_chunk_entries = entries;

  2024.     atom = &trak->stsc_atom_buf;
  2025.     atom->temporary = 1;
  2026.     atom->pos = atom_header;
  2027.     atom->last = atom_table;

  2028.     data = &trak->stsc_data_buf;
  2029.     data->temporary = 1;
  2030.     data->pos = atom_table;
  2031.     data->last = atom_end;

  2032.     trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom;
  2033.     trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data;

  2034.     ngx_mp4_atom_next(mp4, atom_data_size);

  2035.     return NGX_OK;
  2036. }


  2037. static ngx_int_t
  2038. ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
  2039.     ngx_http_mp4_trak_t *trak)
  2040. {
  2041.     size_t                 atom_size;
  2042.     uint32_t               chunk;
  2043.     ngx_buf_t             *atom, *data;
  2044.     ngx_mp4_stsc_atom_t   *stsc_atom;
  2045.     ngx_mp4_stsc_entry_t  *entry, *end;

  2046.     /*
  2047.      * mdia.minf.stbl.stsc updating requires trak->start_sample
  2048.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2049.      * atom which may reside after mdia.minf
  2050.      */

  2051.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2052.                    "mp4 stsc atom update");

  2053.     data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;

  2054.     if (data == NULL) {
  2055.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2056.                       "no mp4 stsc atoms were found in \"%s\"",
  2057.                       mp4->file.name.data);
  2058.         return NGX_ERROR;
  2059.     }

  2060.     if (trak->sample_to_chunk_entries == 0) {
  2061.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2062.                       "zero number of entries in stsc atom in \"%s\"",
  2063.                       mp4->file.name.data);
  2064.         return NGX_ERROR;
  2065.     }

  2066.     if (ngx_http_mp4_crop_stsc_data(mp4, trak, 1) != NGX_OK) {
  2067.         return NGX_ERROR;
  2068.     }

  2069.     if (ngx_http_mp4_crop_stsc_data(mp4, trak, 0) != NGX_OK) {
  2070.         return NGX_ERROR;
  2071.     }

  2072.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2073.                    "sample-to-chunk entries:%uD",
  2074.                    trak->sample_to_chunk_entries);

  2075.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2076.     end = (ngx_mp4_stsc_entry_t *) data->last;

  2077.     while (entry < end) {
  2078.         chunk = ngx_mp4_get_32value(entry->chunk);
  2079.         chunk -= trak->start_chunk;
  2080.         ngx_mp4_set_32value(entry->chunk, chunk);
  2081.         entry++;
  2082.     }

  2083.     atom_size = sizeof(ngx_mp4_stsc_atom_t)
  2084.                 + trak->sample_to_chunk_entries * sizeof(ngx_mp4_stsc_entry_t);

  2085.     trak->size += atom_size;

  2086.     atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf;
  2087.     stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos;

  2088.     ngx_mp4_set_32value(stsc_atom->size, atom_size);
  2089.     ngx_mp4_set_32value(stsc_atom->entries, trak->sample_to_chunk_entries);

  2090.     return NGX_OK;
  2091. }


  2092. static ngx_int_t
  2093. ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
  2094.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  2095. {
  2096.     uint32_t               start_sample, chunk, samples, id, next_chunk, n,
  2097.                            prev_samples;
  2098.     ngx_buf_t             *data, *buf;
  2099.     ngx_uint_t             entries, target_chunk, chunk_samples;
  2100.     ngx_mp4_stsc_entry_t  *entry, *end, *first;

  2101.     entries = trak->sample_to_chunk_entries - 1;

  2102.     if (start) {
  2103.         start_sample = (uint32_t) trak->start_sample;

  2104.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2105.                        "mp4 stsc crop start_sample:%uD", start_sample);

  2106.     } else if (mp4->length) {
  2107.         start_sample = (uint32_t) (trak->end_sample - trak->start_sample);
  2108.         samples = 0;

  2109.         data = trak->out[NGX_HTTP_MP4_STSC_START].buf;

  2110.         if (data) {
  2111.             entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2112.             samples = ngx_mp4_get_32value(entry->samples);
  2113.             entries--;

  2114.             if (samples > start_sample) {
  2115.                 samples = start_sample;
  2116.                 ngx_mp4_set_32value(entry->samples, samples);
  2117.             }

  2118.             start_sample -= samples;
  2119.         }

  2120.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2121.                        "mp4 stsc crop end_sample:%uD, ext_samples:%uD",
  2122.                        start_sample, samples);

  2123.     } else {
  2124.         return NGX_OK;
  2125.     }

  2126.     data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;

  2127.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2128.     end = (ngx_mp4_stsc_entry_t *) data->last;

  2129.     chunk = ngx_mp4_get_32value(entry->chunk);
  2130.     samples = ngx_mp4_get_32value(entry->samples);
  2131.     id = ngx_mp4_get_32value(entry->id);
  2132.     prev_samples = 0;
  2133.     entry++;

  2134.     while (entry < end) {

  2135.         next_chunk = ngx_mp4_get_32value(entry->chunk);

  2136.         ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2137.                        "sample:%uD, chunk:%uD, chunks:%uD, "
  2138.                        "samples:%uD, id:%uD",
  2139.                        start_sample, chunk, next_chunk - chunk, samples, id);

  2140.         n = (next_chunk - chunk) * samples;

  2141.         if (start_sample < n) {
  2142.             goto found;
  2143.         }

  2144.         start_sample -= n;

  2145.         prev_samples = samples;
  2146.         chunk = next_chunk;
  2147.         samples = ngx_mp4_get_32value(entry->samples);
  2148.         id = ngx_mp4_get_32value(entry->id);
  2149.         entries--;
  2150.         entry++;
  2151.     }

  2152.     next_chunk = trak->chunks + 1;

  2153.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2154.                    "sample:%uD, chunk:%uD, chunks:%uD, samples:%uD",
  2155.                    start_sample, chunk, next_chunk - chunk, samples);

  2156.     n = (next_chunk - chunk) * samples;

  2157.     if (start_sample > n) {
  2158.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2159.                       "%s time is out mp4 stsc chunks in \"%s\"",
  2160.                       start ? "start" : "end", mp4->file.name.data);
  2161.         return NGX_ERROR;
  2162.     }

  2163. found:

  2164.     entries++;
  2165.     entry--;

  2166.     if (samples == 0) {
  2167.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2168.                       "zero number of samples in \"%s\"",
  2169.                       mp4->file.name.data);
  2170.         return NGX_ERROR;
  2171.     }

  2172.     target_chunk = chunk - 1;
  2173.     target_chunk += start_sample / samples;
  2174.     chunk_samples = start_sample % samples;

  2175.     if (start) {
  2176.         data->pos = (u_char *) entry;

  2177.         trak->sample_to_chunk_entries = entries;
  2178.         trak->start_chunk = target_chunk;
  2179.         trak->start_chunk_samples = chunk_samples;

  2180.         ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 1);

  2181.         samples -= chunk_samples;

  2182.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2183.                        "start_chunk:%ui, start_chunk_samples:%ui",
  2184.                        trak->start_chunk, trak->start_chunk_samples);

  2185.     } else {
  2186.         if (start_sample) {
  2187.             data->last = (u_char *) (entry + 1);
  2188.             trak->sample_to_chunk_entries -= entries - 1;
  2189.             trak->end_chunk_samples = samples;

  2190.         } else {
  2191.             data->last = (u_char *) entry;
  2192.             trak->sample_to_chunk_entries -= entries;
  2193.             trak->end_chunk_samples = prev_samples;
  2194.         }

  2195.         if (chunk_samples) {
  2196.             trak->end_chunk = target_chunk + 1;
  2197.             trak->end_chunk_samples = chunk_samples;

  2198.         } else {
  2199.             trak->end_chunk = target_chunk;
  2200.         }

  2201.         samples = chunk_samples;
  2202.         next_chunk = chunk + 1;

  2203.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2204.                        "end_chunk:%ui, end_chunk_samples:%ui",
  2205.                        trak->end_chunk, trak->end_chunk_samples);
  2206.     }

  2207.     if (chunk_samples && next_chunk - target_chunk == 2) {

  2208.         ngx_mp4_set_32value(entry->samples, samples);

  2209.     } else if (chunk_samples && start) {

  2210.         first = &trak->stsc_start_chunk_entry;
  2211.         ngx_mp4_set_32value(first->chunk, 1);
  2212.         ngx_mp4_set_32value(first->samples, samples);
  2213.         ngx_mp4_set_32value(first->id, id);

  2214.         buf = &trak->stsc_start_chunk_buf;
  2215.         buf->temporary = 1;
  2216.         buf->pos = (u_char *) first;
  2217.         buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);

  2218.         trak->out[NGX_HTTP_MP4_STSC_START].buf = buf;

  2219.         ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 2);

  2220.         trak->sample_to_chunk_entries++;

  2221.     } else if (chunk_samples) {

  2222.         first = &trak->stsc_end_chunk_entry;
  2223.         ngx_mp4_set_32value(first->chunk, trak->end_chunk - trak->start_chunk);
  2224.         ngx_mp4_set_32value(first->samples, samples);
  2225.         ngx_mp4_set_32value(first->id, id);

  2226.         buf = &trak->stsc_end_chunk_buf;
  2227.         buf->temporary = 1;
  2228.         buf->pos = (u_char *) first;
  2229.         buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);

  2230.         trak->out[NGX_HTTP_MP4_STSC_END].buf = buf;

  2231.         trak->sample_to_chunk_entries++;
  2232.     }

  2233.     return NGX_OK;
  2234. }


  2235. typedef struct {
  2236.     u_char    size[4];
  2237.     u_char    name[4];
  2238.     u_char    version[1];
  2239.     u_char    flags[3];
  2240.     u_char    uniform_size[4];
  2241.     u_char    entries[4];
  2242. } ngx_mp4_stsz_atom_t;


  2243. static ngx_int_t
  2244. ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2245. {
  2246.     u_char               *atom_header, *atom_table, *atom_end;
  2247.     size_t                atom_size;
  2248.     uint32_t              entries, size;
  2249.     ngx_buf_t            *atom, *data;
  2250.     ngx_mp4_stsz_atom_t  *stsz_atom;
  2251.     ngx_http_mp4_trak_t  *trak;

  2252.     /* sample sizes atom */

  2253.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsz atom");

  2254.     atom_header = ngx_mp4_atom_header(mp4);
  2255.     stsz_atom = (ngx_mp4_stsz_atom_t *) atom_header;
  2256.     ngx_mp4_set_atom_name(stsz_atom, 's', 't', 's', 'z');

  2257.     if (ngx_mp4_atom_data_size(ngx_mp4_stsz_atom_t) > atom_data_size) {
  2258.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2259.                       "\"%s\" mp4 stsz atom too small", mp4->file.name.data);
  2260.         return NGX_ERROR;
  2261.     }

  2262.     size = ngx_mp4_get_32value(stsz_atom->uniform_size);
  2263.     entries = ngx_mp4_get_32value(stsz_atom->entries);

  2264.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2265.                    "sample uniform size:%uD, entries:%uD", size, entries);

  2266.     trak = ngx_mp4_last_trak(mp4);
  2267.     trak->sample_sizes_entries = entries;

  2268.     atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t);

  2269.     atom = &trak->stsz_atom_buf;
  2270.     atom->temporary = 1;
  2271.     atom->pos = atom_header;
  2272.     atom->last = atom_table;

  2273.     trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf = atom;

  2274.     if (size == 0) {
  2275.         if (ngx_mp4_atom_data_size(ngx_mp4_stsz_atom_t)
  2276.             + entries * sizeof(uint32_t) > atom_data_size)
  2277.         {
  2278.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2279.                           "\"%s\" mp4 stsz atom too small",
  2280.                           mp4->file.name.data);
  2281.             return NGX_ERROR;
  2282.         }

  2283.         atom_end = atom_table + entries * sizeof(uint32_t);

  2284.         data = &trak->stsz_data_buf;
  2285.         data->temporary = 1;
  2286.         data->pos = atom_table;
  2287.         data->last = atom_end;

  2288.         trak->out[NGX_HTTP_MP4_STSZ_DATA].buf = data;

  2289.     } else {
  2290.         /* if size != 0 then all samples are the same size */
  2291.         /* TODO : chunk samples */
  2292.         atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  2293.         ngx_mp4_set_32value(atom_header, atom_size);
  2294.         trak->size += atom_size;
  2295.     }

  2296.     ngx_mp4_atom_next(mp4, atom_data_size);

  2297.     return NGX_OK;
  2298. }


  2299. static ngx_int_t
  2300. ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
  2301.     ngx_http_mp4_trak_t *trak)
  2302. {
  2303.     size_t                atom_size;
  2304.     uint32_t             *pos, *end, entries;
  2305.     ngx_buf_t            *atom, *data;
  2306.     ngx_mp4_stsz_atom_t  *stsz_atom;

  2307.     /*
  2308.      * mdia.minf.stbl.stsz updating requires trak->start_sample
  2309.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2310.      * atom which may reside after mdia.minf
  2311.      */

  2312.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2313.                    "mp4 stsz atom update");

  2314.     data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf;

  2315.     if (data) {
  2316.         entries = trak->sample_sizes_entries;

  2317.         if (trak->start_sample > entries) {
  2318.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2319.                           "start time is out mp4 stsz samples in \"%s\"",
  2320.                           mp4->file.name.data);
  2321.             return NGX_ERROR;
  2322.         }

  2323.         entries -= trak->start_sample;
  2324.         data->pos += trak->start_sample * sizeof(uint32_t);
  2325.         end = (uint32_t *) data->pos;

  2326.         for (pos = end - trak->start_chunk_samples; pos < end; pos++) {
  2327.             trak->start_chunk_samples_size += ngx_mp4_get_32value(pos);
  2328.         }

  2329.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2330.                        "chunk samples sizes:%uL",
  2331.                        trak->start_chunk_samples_size);

  2332.         if (mp4->length) {
  2333.             if (trak->end_sample - trak->start_sample > entries) {
  2334.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2335.                               "end time is out mp4 stsz samples in \"%s\"",
  2336.                               mp4->file.name.data);
  2337.                 return NGX_ERROR;
  2338.             }

  2339.             entries = trak->end_sample - trak->start_sample;
  2340.             data->last = data->pos + entries * sizeof(uint32_t);
  2341.             end = (uint32_t *) data->last;

  2342.             for (pos = end - trak->end_chunk_samples; pos < end; pos++) {
  2343.                 trak->end_chunk_samples_size += ngx_mp4_get_32value(pos);
  2344.             }

  2345.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2346.                            "mp4 stsz end_chunk_samples_size:%uL",
  2347.                            trak->end_chunk_samples_size);
  2348.         }

  2349.         atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos);
  2350.         trak->size += atom_size;

  2351.         atom = trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf;
  2352.         stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos;

  2353.         ngx_mp4_set_32value(stsz_atom->size, atom_size);
  2354.         ngx_mp4_set_32value(stsz_atom->entries, entries);
  2355.     }

  2356.     return NGX_OK;
  2357. }


  2358. typedef struct {
  2359.     u_char    size[4];
  2360.     u_char    name[4];
  2361.     u_char    version[1];
  2362.     u_char    flags[3];
  2363.     u_char    entries[4];
  2364. } ngx_mp4_stco_atom_t;


  2365. static ngx_int_t
  2366. ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2367. {
  2368.     u_char               *atom_header, *atom_table, *atom_end;
  2369.     uint32_t              entries;
  2370.     ngx_buf_t            *atom, *data;
  2371.     ngx_mp4_stco_atom_t  *stco_atom;
  2372.     ngx_http_mp4_trak_t  *trak;

  2373.     /* chunk offsets atom */

  2374.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stco atom");

  2375.     atom_header = ngx_mp4_atom_header(mp4);
  2376.     stco_atom = (ngx_mp4_stco_atom_t *) atom_header;
  2377.     ngx_mp4_set_atom_name(stco_atom, 's', 't', 'c', 'o');

  2378.     if (ngx_mp4_atom_data_size(ngx_mp4_stco_atom_t) > atom_data_size) {
  2379.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2380.                       "\"%s\" mp4 stco atom too small", mp4->file.name.data);
  2381.         return NGX_ERROR;
  2382.     }

  2383.     entries = ngx_mp4_get_32value(stco_atom->entries);

  2384.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries);

  2385.     if (ngx_mp4_atom_data_size(ngx_mp4_stco_atom_t)
  2386.         + entries * sizeof(uint32_t) > atom_data_size)
  2387.     {
  2388.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2389.                       "\"%s\" mp4 stco atom too small", mp4->file.name.data);
  2390.         return NGX_ERROR;
  2391.     }

  2392.     atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t);
  2393.     atom_end = atom_table + entries * sizeof(uint32_t);

  2394.     trak = ngx_mp4_last_trak(mp4);
  2395.     trak->chunks = entries;

  2396.     atom = &trak->stco_atom_buf;
  2397.     atom->temporary = 1;
  2398.     atom->pos = atom_header;
  2399.     atom->last = atom_table;

  2400.     data = &trak->stco_data_buf;
  2401.     data->temporary = 1;
  2402.     data->pos = atom_table;
  2403.     data->last = atom_end;

  2404.     trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom;
  2405.     trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data;

  2406.     ngx_mp4_atom_next(mp4, atom_data_size);

  2407.     return NGX_OK;
  2408. }


  2409. static ngx_int_t
  2410. ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
  2411.     ngx_http_mp4_trak_t *trak)
  2412. {
  2413.     size_t                atom_size;
  2414.     uint32_t              entries;
  2415.     ngx_buf_t            *atom, *data;
  2416.     ngx_mp4_stco_atom_t  *stco_atom;

  2417.     /*
  2418.      * mdia.minf.stbl.stco updating requires trak->start_chunk
  2419.      * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd
  2420.      * atom which may reside after mdia.minf
  2421.      */

  2422.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2423.                    "mp4 stco atom update");

  2424.     data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;

  2425.     if (data == NULL) {
  2426.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2427.                       "no mp4 stco atoms were found in \"%s\"",
  2428.                       mp4->file.name.data);
  2429.         return NGX_ERROR;
  2430.     }

  2431.     if (trak->start_chunk > trak->chunks) {
  2432.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2433.                       "start time is out mp4 stco chunks in \"%s\"",
  2434.                       mp4->file.name.data);
  2435.         return NGX_ERROR;
  2436.     }

  2437.     data->pos += trak->start_chunk * sizeof(uint32_t);

  2438.     trak->start_offset = ngx_mp4_get_32value(data->pos);
  2439.     trak->start_offset += trak->start_chunk_samples_size;
  2440.     ngx_mp4_set_32value(data->pos, trak->start_offset);

  2441.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2442.                    "start chunk offset:%O", trak->start_offset);

  2443.     if (mp4->length) {

  2444.         if (trak->end_chunk > trak->chunks) {
  2445.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2446.                           "end time is out mp4 stco chunks in \"%s\"",
  2447.                           mp4->file.name.data);
  2448.             return NGX_ERROR;
  2449.         }

  2450.         entries = trak->end_chunk - trak->start_chunk;
  2451.         data->last = data->pos + entries * sizeof(uint32_t);

  2452.         if (entries) {
  2453.             trak->end_offset =
  2454.                             ngx_mp4_get_32value(data->last - sizeof(uint32_t));
  2455.             trak->end_offset += trak->end_chunk_samples_size;

  2456.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2457.                            "end chunk offset:%O", trak->end_offset);
  2458.         }

  2459.     } else {
  2460.         entries = trak->chunks - trak->start_chunk;
  2461.         trak->end_offset = mp4->mdat_data.buf->file_last;
  2462.     }

  2463.     if (entries == 0) {
  2464.         trak->start_offset = mp4->end;
  2465.         trak->end_offset = 0;
  2466.     }

  2467.     atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos);
  2468.     trak->size += atom_size;

  2469.     atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf;
  2470.     stco_atom = (ngx_mp4_stco_atom_t *) atom->pos;

  2471.     ngx_mp4_set_32value(stco_atom->size, atom_size);
  2472.     ngx_mp4_set_32value(stco_atom->entries, entries);

  2473.     return NGX_OK;
  2474. }


  2475. static void
  2476. ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
  2477.     ngx_http_mp4_trak_t *trak, int32_t adjustment)
  2478. {
  2479.     uint32_t    offset, *entry, *end;
  2480.     ngx_buf_t  *data;

  2481.     /*
  2482.      * moov.trak.mdia.minf.stbl.stco adjustment requires
  2483.      * minimal start offset of all traks and new moov atom size
  2484.      */

  2485.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2486.                    "mp4 stco atom adjustment");

  2487.     data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;
  2488.     entry = (uint32_t *) data->pos;
  2489.     end = (uint32_t *) data->last;

  2490.     while (entry < end) {
  2491.         offset = ngx_mp4_get_32value(entry);
  2492.         offset += adjustment;
  2493.         ngx_mp4_set_32value(entry, offset);
  2494.         entry++;
  2495.     }
  2496. }


  2497. typedef struct {
  2498.     u_char    size[4];
  2499.     u_char    name[4];
  2500.     u_char    version[1];
  2501.     u_char    flags[3];
  2502.     u_char    entries[4];
  2503. } ngx_mp4_co64_atom_t;


  2504. static ngx_int_t
  2505. ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2506. {
  2507.     u_char               *atom_header, *atom_table, *atom_end;
  2508.     uint32_t              entries;
  2509.     ngx_buf_t            *atom, *data;
  2510.     ngx_mp4_co64_atom_t  *co64_atom;
  2511.     ngx_http_mp4_trak_t  *trak;

  2512.     /* chunk offsets atom */

  2513.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 co64 atom");

  2514.     atom_header = ngx_mp4_atom_header(mp4);
  2515.     co64_atom = (ngx_mp4_co64_atom_t *) atom_header;
  2516.     ngx_mp4_set_atom_name(co64_atom, 'c', 'o', '6', '4');

  2517.     if (ngx_mp4_atom_data_size(ngx_mp4_co64_atom_t) > atom_data_size) {
  2518.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2519.                       "\"%s\" mp4 co64 atom too small", mp4->file.name.data);
  2520.         return NGX_ERROR;
  2521.     }

  2522.     entries = ngx_mp4_get_32value(co64_atom->entries);

  2523.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries);

  2524.     if (ngx_mp4_atom_data_size(ngx_mp4_co64_atom_t)
  2525.         + entries * sizeof(uint64_t) > atom_data_size)
  2526.     {
  2527.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2528.                       "\"%s\" mp4 co64 atom too small", mp4->file.name.data);
  2529.         return NGX_ERROR;
  2530.     }

  2531.     atom_table = atom_header + sizeof(ngx_mp4_co64_atom_t);
  2532.     atom_end = atom_table + entries * sizeof(uint64_t);

  2533.     trak = ngx_mp4_last_trak(mp4);
  2534.     trak->chunks = entries;

  2535.     atom = &trak->co64_atom_buf;
  2536.     atom->temporary = 1;
  2537.     atom->pos = atom_header;
  2538.     atom->last = atom_table;

  2539.     data = &trak->co64_data_buf;
  2540.     data->temporary = 1;
  2541.     data->pos = atom_table;
  2542.     data->last = atom_end;

  2543.     trak->out[NGX_HTTP_MP4_CO64_ATOM].buf = atom;
  2544.     trak->out[NGX_HTTP_MP4_CO64_DATA].buf = data;

  2545.     ngx_mp4_atom_next(mp4, atom_data_size);

  2546.     return NGX_OK;
  2547. }


  2548. static ngx_int_t
  2549. ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
  2550.     ngx_http_mp4_trak_t *trak)
  2551. {
  2552.     size_t                atom_size;
  2553.     uint64_t              entries;
  2554.     ngx_buf_t            *atom, *data;
  2555.     ngx_mp4_co64_atom_t  *co64_atom;

  2556.     /*
  2557.      * mdia.minf.stbl.co64 updating requires trak->start_chunk
  2558.      * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd
  2559.      * atom which may reside after mdia.minf
  2560.      */

  2561.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2562.                    "mp4 co64 atom update");

  2563.     data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf;

  2564.     if (data == NULL) {
  2565.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2566.                       "no mp4 co64 atoms were found in \"%s\"",
  2567.                       mp4->file.name.data);
  2568.         return NGX_ERROR;
  2569.     }

  2570.     if (trak->start_chunk > trak->chunks) {
  2571.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2572.                       "start time is out mp4 co64 chunks in \"%s\"",
  2573.                       mp4->file.name.data);
  2574.         return NGX_ERROR;
  2575.     }

  2576.     data->pos += trak->start_chunk * sizeof(uint64_t);

  2577.     trak->start_offset = ngx_mp4_get_64value(data->pos);
  2578.     trak->start_offset += trak->start_chunk_samples_size;
  2579.     ngx_mp4_set_64value(data->pos, trak->start_offset);

  2580.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2581.                    "start chunk offset:%O", trak->start_offset);

  2582.     if (mp4->length) {

  2583.         if (trak->end_chunk > trak->chunks) {
  2584.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2585.                           "end time is out mp4 co64 chunks in \"%s\"",
  2586.                           mp4->file.name.data);
  2587.             return NGX_ERROR;
  2588.         }

  2589.         entries = trak->end_chunk - trak->start_chunk;
  2590.         data->last = data->pos + entries * sizeof(uint64_t);

  2591.         if (entries) {
  2592.             trak->end_offset =
  2593.                             ngx_mp4_get_64value(data->last - sizeof(uint64_t));
  2594.             trak->end_offset += trak->end_chunk_samples_size;

  2595.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2596.                            "end chunk offset:%O", trak->end_offset);
  2597.         }

  2598.     } else {
  2599.         entries = trak->chunks - trak->start_chunk;
  2600.         trak->end_offset = mp4->mdat_data.buf->file_last;
  2601.     }

  2602.     if (entries == 0) {
  2603.         trak->start_offset = mp4->end;
  2604.         trak->end_offset = 0;
  2605.     }

  2606.     atom_size = sizeof(ngx_mp4_co64_atom_t) + (data->last - data->pos);
  2607.     trak->size += atom_size;

  2608.     atom = trak->out[NGX_HTTP_MP4_CO64_ATOM].buf;
  2609.     co64_atom = (ngx_mp4_co64_atom_t *) atom->pos;

  2610.     ngx_mp4_set_32value(co64_atom->size, atom_size);
  2611.     ngx_mp4_set_32value(co64_atom->entries, entries);

  2612.     return NGX_OK;
  2613. }


  2614. static void
  2615. ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4,
  2616.     ngx_http_mp4_trak_t *trak, off_t adjustment)
  2617. {
  2618.     uint64_t    offset, *entry, *end;
  2619.     ngx_buf_t  *data;

  2620.     /*
  2621.      * moov.trak.mdia.minf.stbl.co64 adjustment requires
  2622.      * minimal start offset of all traks and new moov atom size
  2623.      */

  2624.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2625.                    "mp4 co64 atom adjustment");

  2626.     data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf;
  2627.     entry = (uint64_t *) data->pos;
  2628.     end = (uint64_t *) data->last;

  2629.     while (entry < end) {
  2630.         offset = ngx_mp4_get_64value(entry);
  2631.         offset += adjustment;
  2632.         ngx_mp4_set_64value(entry, offset);
  2633.         entry++;
  2634.     }
  2635. }


  2636. static char *
  2637. ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  2638. {
  2639.     ngx_http_core_loc_conf_t  *clcf;

  2640.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  2641.     clcf->handler = ngx_http_mp4_handler;

  2642.     return NGX_CONF_OK;
  2643. }


  2644. static void *
  2645. ngx_http_mp4_create_conf(ngx_conf_t *cf)
  2646. {
  2647.     ngx_http_mp4_conf_t  *conf;

  2648.     conf = ngx_palloc(cf->pool, sizeof(ngx_http_mp4_conf_t));
  2649.     if (conf == NULL) {
  2650.         return NULL;
  2651.     }

  2652.     conf->buffer_size = NGX_CONF_UNSET_SIZE;
  2653.     conf->max_buffer_size = NGX_CONF_UNSET_SIZE;

  2654.     return conf;
  2655. }


  2656. static char *
  2657. ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  2658. {
  2659.     ngx_http_mp4_conf_t *prev = parent;
  2660.     ngx_http_mp4_conf_t *conf = child;

  2661.     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, 512 * 1024);
  2662.     ngx_conf_merge_size_value(conf->max_buffer_size, prev->max_buffer_size,
  2663.                               10 * 1024 * 1024);

  2664.     return NGX_CONF_OK;
  2665. }